ISR and data fetching
Hi!
I noticed on the TanStack website that you’ve implemented something similar to ISR (Incremental Static Regeneration) using CDN headers, like this:
headers: (ctx) => {
return {
'cache-control': 'public, max-age=0, must-revalidate',
'cdn-cache-control': 'max-age=300, stale-while-revalidate=300, durable',
}
}
What I’m trying to achieve is this:
I have an external API that I want to call only once per day. After that, I want the result to be cached at the CDN level and used like an ISR response for any future requests.
The problem I’m running into is that when navigating on the client side , the loaders run on the client, which means:
Extra requests may be made directly from the browser.
The client-side data may differ from the server/CDN cached version.
what can I do
4 Replies
optimistic-gold•5mo ago
📌 interested what advice you get here from others as i'm 100% new here, the loaders certinaly run on the client so that sounds like intended behaviour.
as far as the in a deployed version if your whole deployment is behind a cdn the cdn I assume would respect your cache rules and reply without hitting the server function or api route that the loader is calling.
but again please not i'm very new here.
I'm was just about to ask when are we expecting custom cache rules for server functions / api routes but hadn't considered having a cdn do it for you if thats easy.
foreign-sapphireOP•5mo ago
One approach I’ve been thinking about is to move the data fetching into a server function, and then call that function inside the loader.
That way, the server function becomes the only place hitting the external API, and we can apply proper CDN caching headers to its response — essentially caching that result at the edge.
Then even when the loader runs on the client, it’s just calling that server function, and the CDN can serve a cached response if available.
But again, it feels more like a workaround than a clean solution.
optimistic-gold•5mo ago
yeah would be nice to know the blessed path recomended.
what's your plan on your deploy target? i.e. cloudflair+workers, or a node-server? etc?
foreign-sapphireOP•5mo ago
I prefer Netlify or Vercel