2

i am new to NextJs and I'm tryin to make an app that every few hours make some requests to a database to update the data, but i can't figure out how to do it. The only place where i can do it is inside the pages components, but that means that there has to be a request from a client in order to trigger the update script but i want it to be executed continuously when the server starts and every few hours after that.

  • 1
    https://vercel.com/docs/concepts/solutions/cron-jobs – Quentin May 16 '22 at 08:37
  • Look through the link in the comment above. My suggestion is to use GitHub Actions since it's free and works alongside your version control. Here's an example of a real usage: https://github.com/abir-taheer/vote.stuysu.org/blob/main/.github/workflows/cron.yaml – Abir Taheer May 16 '22 at 08:39
  • Are you using static generation in your Next.js app? Have you looked into [Incremental Static Regeneration](https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration) to update the static pages? – juliomalves May 16 '22 at 21:14

1 Answers1

0

I think you have two options you can either use vercel's cron method. https://vercel.com/docs/concepts/solutions/cron-jobs

What I'm more comfortable with is, using some setInterval logic inside my app.ts (app.js) where its mere a express server starting point. It will keep fetching the data with setInterval. we might use some pm2logic here as well.

Sohail Haider
  • 166
  • 1
  • 11
  • yeah actually i too prefer the setInterval logic, but how exactly could i do that? By creating an express server inside the same app or creating a separate app, which will communicate with my nextjs app? – Chris Tsironis May 16 '22 at 13:47
  • nextjs already have express server inside it. you can view app.ts file there you will see its actually an express server underneath. – Sohail Haider Jun 02 '22 at 16:41