Get data from API every x time

I’d recommend a cron job for this instead of SvelteKit.

If you’re starting your data polling on the server side in SvelteKit, the data polling may only last while users are online or once per-session (it really depends on where your code for this is located in your SvelteKit app). The polling definitely will not run consistently if deploying to Vercel/Netlify or similar with Serverless or Edge Functions, because the functions only run temporarily to build pages and handle routes. There’s a risk for inconsistent or no routine fetching at all, and your database data could become stale for certain periods of time.

In this case:

  1. The job is strictly related to the database and does not need to touch or reference the client session whatsoever

  2. The job needs to be consistently/periodically updating the database with data—again not tied to the client in any way

  3. The job should probably still run even if the client is down or offline for whatever reason

It makes more sense to do this on a dedicated server instance that is consistently up and running and not bound to the client. It’s also a non-intensive task to run and could be hosted for really cheap. Some database services have cron jobs built-in too. Render has cron jobs you can spin up. You could even use your own server.

/r/sveltejs Thread Parent