5

I am trying to add a guardian process for my NextJS application. it will start with the launch of my nextjs application, and then perform some jobs regularly(like backup data).

But I didn't find some "callback" or "eventListener" to let me call my startup function.

I tried to execute the startup function in pages/_app.tsx, but it was called every time when user accessed it, not only once when the nextjs application started.

The following is my expectation:

  • Call only once when the entire nextjs application is started.
  • No need to use a custom backend.(There are already a lot of code accumulated on the default server of Next)
  • Can use the ability provided by Next, such as TypeScript, Babel, export / import, etc.
  • The code in the nextjs application can directly Import related its function and use.

Can anyone provide some inspiration?

hopgoldy
  • 85
  • 2
  • 7
  • getStaticProps and getStaticPaths could be what you're looking for but it's hard to say without additional context. – kelsny Apr 24 '22 at 12:32
  • Does this help answer your question: [Next.js run function/script when starting app](https://stackoverflow.com/questions/62134289/next-js-run-function-script-when-starting-app)? – juliomalves Apr 26 '22 at 17:55
  • Thank you for your link @juliomalves, but I have seen this. the cost of switching to the custom server is too great, and using Webpack to call the startup function will cause other code cannot be directly `import` related methods – hopgoldy Apr 28 '22 at 08:29
  • use the next/script `Script` component (call you data in onLoad function). to run only single time, use localStorage. – Sunny Goel Aug 01 '22 at 18:24

0 Answers0