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 require("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
require("anay-208")
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
require("anay-208")
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
require("anay-208")
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
require("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
Answer
Avatar
require("anay-208")
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
@require("anay-208") Thank you!
Avatar
require("anay-208")
can you mark my message as a solution?