How implement deploy tracking on Next.js without a custom server?

455 Views Asked by At

I'm working on a Next.js project and we're running it WITHOUT the custom server, using the next build + next start.

On the backend, when a new version is deployed and starts to run, we send a log to a service warning the team of the success of the deploy, so we want to do the same for the Next.js frontend.

My question is: where can we add this log that will only be send when the build is already finished and the new version is running for the first time (with success)?

  • Would be nice if this log is only send a single time for each new build;
  • Also, this log need to be send only on runtime, and not on build time;
  • One more thing that would be nice for the deploy tracking is to send the log only when the frontend successfully load;

With the custom server, is enough for us if we log on the app.prepare, but we're really trying to skip the implementation of the custom server.

There's a central point/callback that is only called a single time after the yarn start?

1

There are 1 best solutions below

0
assisrMatheus On

One option is to create a custom next.config.js, and listen for the phase-production-server phase.

const { PHASE_PRODUCTION_SERVER } = require('next/constants');

module.exports = (phase, { defaultConfig }) => {
  if (phase === PHASE_PRODUCTION_SERVER) {
    // Post to an API
  }

  return defaultConfig;
};

It's not perfect but I'm not sure there's a handler for when the server is ready. I'm also curious about that and asked a similar-but-different question in their discussion section.

You can read more about phase here.