Caching of external api calls from within SvelteKit
See original GitHub issueDescribe the problem
The following situation:
On my new personal website, I wanted to call the dev.to api to show the links in the blog section.
I made it work by adding an json endpoint and including it in the SSR.
The problem I see is that with every request to /blog
the dev.to api will be called again (with a lot of traffic the might block me).
So if I could cache the response for about 1 hour, I could drastically reduce the api calls even when a lot of traffic hits.
As far as I understand it cloudflare has some functionallity like that.
Describe the proposed solution
I would be nice to have something similar to cloudflare, where we use cache controll on the request/response.
Alternatives considered
No response
Importance
nice to have
Additional Information
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:13 (4 by maintainers)
Top Results From Across the Web
Best approach to use external API data like session cache?
I have a sveltekit endpoint that fetches data from an external API, something like /data.json.ts. I have multiple pages in my app and...
Read more >Svelte: is there a way to cache the api result in ...
My purpose is to fetch blogposts from an external API and show them in a list but not on every refresh, or link...
Read more >Loading data • Docs • SvelteKit
Universal load functions are useful when you need to fetch data from an external API and don't need private credentials, since SvelteKit can...
Read more >Svelte Query - Hooks for fetching, caching and updating ...
Performant and powerful data synchronization for Svelte. Fetch, cache and update data in your Svelte applications all without touching any "global state".
Read more >Cache a response in Vercel using SSR and serverless ...
TLDR I will show how we can cache the response of an API using Vercel caching ... in Vercel using SSR and serverless...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You have a variety of options, depending on exactly what behaviour you want. You can return a cache-control header from your endpoint…
…or, if you wanted to respect the original cache headers (probably not, since it looks like the dev.to API uses
public, no-cache
), and you don’t need to munge the response in any way, you can just proxy it……or, since the API doesn’t need a private key, you could fetch the data in
load
and usemaxage
, which will hit the API during SSR but then cache the rendered page, and hit the API directly for client-side navigation rather than going via your endpoint (which will result in more API hits if people navigate between blog posts, but from many IP addresses):Personally I’d probably opt for the first one, since it means you have the opportunity to slim down the response to just the bits you need. Though I’d typically pick a much lower maxage (a few minutes at most, rather than an hour), so that you can handle getting slashdotted (oops, might have just aged myself) while avoiding stale content.
It depends.
Cache-Control: max-age=3600
will be respected by browsers, butCache-Control: public, max-age=3600
will be respected by CDNs as well — in other words, if you have a CDN in front of your app, then only the first request in a ten minute window should hit the origin server. (Subsequent requests will have an additionalAge
header, so that a response served 7 minutes into the window will only be cached by the browser for the remaining 3 minutes.)Yes, we definitely want to adopt ISR (particularly on-demand ISR, where revalidation is triggered by a webhook notifying of a change, rather than requests). It’s something we hope to implement after 1.0: https://github.com/sveltejs/kit/issues/661