How to run a script after running next.js?
Answered
Asiatic Lion posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
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
14 Replies
![Avatar](https://cdn.discordapp.com/avatars/755810867878297610/5ab9ece60bc14c554a7376a599d3deb3.webp?size=256)
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](https://cdn.discordapp.com/embed/avatars/5.png)
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](https://cdn.discordapp.com/avatars/755810867878297610/5ab9ece60bc14c554a7376a599d3deb3.webp?size=256)
Anay-208
Why don’t you cache using nextjs?
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Asiatic LionOP
Because the first request will be from the user. I want to cache after running next.js.
![Avatar](https://cdn.discordapp.com/avatars/755810867878297610/5ab9ece60bc14c554a7376a599d3deb3.webp?size=256)
Anay-208
After 1 request, remaining will be cached for x amount of time.
![Avatar](https://cdn.discordapp.com/embed/avatars/5.png)
Asiatic LionOP
Yes, I know. But I want to cache data after the running application.
![Avatar](https://cdn.discordapp.com/avatars/755810867878297610/5ab9ece60bc14c554a7376a599d3deb3.webp?size=256)
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](https://cdn.discordapp.com/avatars/755810867878297610/5ab9ece60bc14c554a7376a599d3deb3.webp?size=256)
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](https://cdn.discordapp.com/embed/avatars/5.png)
Asiatic LionOP
@Anay-208 Thank you!
![Avatar](https://cdn.discordapp.com/avatars/755810867878297610/5ab9ece60bc14c554a7376a599d3deb3.webp?size=256)
Anay-208
can you mark my message as a solution?