Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache runtime routes (ISR) #198

Open
jbmoelker opened this issue Nov 3, 2024 · 0 comments
Open

Cache runtime routes (ISR) #198

jbmoelker opened this issue Nov 3, 2024 · 0 comments
Labels
enhancement New feature or request performance

Comments

@jbmoelker
Copy link
Member

All HTML routes get a cache-control: public, max-age=0, must-revalidate header from Cloudflare. For our pages statically generated during build this is fine. Cloudflare's CDN and caching mechanism has us covered. For pages that are rendered during runtime the responses will be slower and will hit the underlying services (like DatoCMS) more. We can improve that performance by adding caching headers in an Incremental Static Rendering (ISR) pattern. This blog post explains this pattern using Astro and Cloudflare well: https://logsnag.com/blog/implementing-isr-in-astro

In short, on routes we want to add caching to we can do something like this:

---
// Set Cache-Control headers for CDN caching
Astro.response.headers.set('Cache-Control', 'public, s-maxage=60, stale-while-revalidate=30');
---

And we could add a helper to set that header (as lib or more conveniently on Astro.locals as we need access to the Astro object), like so: (Astro.locals.)setCache({ seconds: 60 }).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance
Projects
None yet
Development

No branches or pull requests

1 participant