Hi!
I am pretty new to the concept of ISR. I want to speed up my build process of my Netlify + Astro site. The site runs in static mode. The files a fetched from a Kirby installation with JSON files.
I have already seen the blog post - but can’t really make sense of it. Mostly it’s about SSR.
Can i put this code into my BaseLayout.astro frontmatter and thats is?
Astro.response.headers.set(
'Cache-Control',
'public, max-age=300, s-maxage=3600'
);
How can I check if it’s working?
Thanks for any help
Disclaimer: You can either choose between long build times or fast responses, not both.
When you build in static mode, all assets are generated during the build time and deployed to the CDN. The server response time would be much faster for the most part. As soon as you switch to rendering on the server, you introduce the following issues:
- Extra load time as server would have to receive the data from your API and generate the page on-the-fly. This can be improved with SWR, but more on that later.
- If your API goes down or can’t handle the load, your site will also go down. Caching can partly solve this, but it’s not a good idea to rely completely on it, details about that will follow.
- You have to rely on caching for the performance which is not completely in your control.
Caching on Netlify is dynamic and local to a CDN node. Each request you make can be handled by one of our several CDN nodes. If a request is not cached in the node that ends up responding to a request, it would re-run the server-side functionality and you’d get a slow response. You can partly solve this problem by using durable caching: Netlify Announces Durable Caching Primitive | Blog.
However, caching is dynamic and limited. All the sites on Netlify are competiting against each other for a space in cache. Thus, if your site is not actively used on a specific CDN node, it would be dropped from the cache. As a result, you would see that your cached ISR data might expire before the time you set, just because it doesn’t exist in the CDN cache anymore.
Moving to your specific questions… To use ISR, you should be using SSR: Routing | Docs (astro.build) and On-demand Rendering Adapters | Docs (astro.build). Then, you can use Astro response headers like you’ve used above: API Reference | Docs (astro.build). To check if it’s working:
- You should see that response header when you check the request in dev tools.
- Repeated requests to the same URL that respond with
cache-status: Netlify Edge hit should:
- be faster
- should not invoke your SSR Function. You should be able to verify it by checking the Function logs.