Next.js Discord

Discord Forum

How to run a script after running next.js?

Answered
Asiatic Lion posted this in #help-forum
Open in Discord
Avatar
Asiatic LionOP
I have next.js application and have cron.js script. I want to run cron.js script only after command next dev. How I can to do this?
Answered by Anay-208
So what you do, first set a config, for app to be cached. Then cron job can send the request to cache the results in server side. I'll tell you in a min on running cron.js after nextjs app is started
View full answer

14 Replies

Avatar
what do you need to run it for?
and why don't you use concurrently?
and set your script to this. it'll run concurrently.
"scripts": {
  "dev": "concurrently \"next dev\" \"node cron.js\""
}
Avatar
Asiatic LionOP
I have a route that takes 5-10 seconds to process, this is not very good for users. Therefore, when I start the Next application, I want to immediately launch cron, which will send a request to this route, in which the data will be cached. This way, when the user opens the page, he will immediately get the result from the cache.
I don't use concurrently because I want run cron only after running next.js.
Avatar
Why don’t you cache using nextjs?
Avatar
Asiatic LionOP
Because the first request will be from the user. I want to cache after running next.js.
Avatar
After 1 request, remaining will be cached for x amount of time.
Avatar
Asiatic LionOP
Yes, I know. But I want to cache data after the running application.
Avatar
So what you do, first set a config, for app to be cached. Then cron job can send the request to cache the results in server side. I'll tell you in a min on running cron.js after nextjs app is started
Answer
Avatar
so you can use concurrency, and use setTimeout every 5s waiting for app to be started, and send a fetch request, and exit the app once you get status 200
@Asiatic Lion
Avatar
Asiatic LionOP
@Anay-208 Thank you!
Avatar
can you mark my message as a solution?