2

How do I hook into the startup of a NextJS server so that I can do one-off initialization?

There's a discussion here but so far no solution.

Guy
  • 65,082
  • 97
  • 254
  • 325
  • if you have your own rollup, you can `npm i -D @rollup/plugin-json`. But if you don't have your own build, then maybe the easiest is to convert it into a js file. Check https://stackoverflow.com/questions/34944099/how-to-import-a-json-file-in-ecmascript-6 for all options. – windmaomao Nov 15 '21 at 20:59
  • Here's a discussion along the lines of what I'm looking for: https://github.com/vercel/next.js/discussions/11686 – Guy Nov 16 '21 at 20:55

2 Answers2

3

Just add your code to the next.config.js file.

Something like this should do the trick:

//next.config.js
//imports

module.exports = async (phase, { defaultConfig }) => {
    const nextConfig = {...}
    console.log("Executes at server startup")
    return nextConfig
}
  • This one works, thank you!! It does seem to run multiple times in next dev, but it only runs once on startup in the actual production build – dominik Mar 21 '23 at 21:42
  • @dominik you're correct, I've noticed this in Next too. For example, whenever a component is mounted in DEV Next unmounts and remounts directly (check console.log in useEffect). You can read about it here: https://legacy.reactjs.org/docs/strict-mode.html#ensuring-reusable-state Not sure if the multiple runs you see is in any way related to this specifically, but many times next automatically tries to simulate specific scenarios and you might see odd behaviour because of that (only in dev). – Fabian Svensson Mar 24 '23 at 08:19
  • If you add "things" to config, the concept of "plugins" feels appropriate. Here an example: https://github.com/tamagui/tamagui/blob/master/starters/next-expo-solito/apps/next/next.config.js – faebster Jul 13 '23 at 01:53
  • @FabianSvensson unfortunately, this is not working for us: we build with output "standalone" and start with `node server.js`. This is next's server.js which is obviously created at build time and contains the hard coded config object. Could you maybe add more details of your config? – Risadinha Jul 28 '23 at 11:54
1

I hope this solution will solve your problem.

This is an example of the startup function in next JS with not custom server

This repository run startup function without using custom server. Before and after the next JS is loaded, it can be executed respectively.

// before next loading

console.log(process.env.ENV_TEST, '[expected]: undefined') // undefined

// after next loading

const command = process.argv[2]
if (['dev', 'start'].includes(command)) {
  require('./node_modules/next/dist/bin/next')
  check(init)
}

// function
// function
// function

// next loading check
/** @param {Function} fn */
function check(fn) {
  setTimeout(() => {
    if (!('LOAD_CHECK' in process.env)) {
      setTimeout(check, 0, fn)
      return
    }
    fn()
  }, 50)
}

// after init function
function init() {
  switch (command) {
    case 'dev': {
      // here dev script
      console.log('run dev')
      break
    }
    case 'start': {
      // here start script
      console.log('run start')
      break
    }
  }
  // commons script
  console.log(process.env.ENV_TEST, '[expected]: Hello!!') // 'Hello!!'
}

For more information, see the app.js in this repository