TLDR in comment
Hi,
I have an Astro project in SSR mode which uses the netlify adapter. It seems to fire the entry lambda function every time a request is made to the site. This function fetches a Wordpress API for every page request that takes about 3 seconds to resolve. It then builds the requested page based on the resolved data.
Expected behavior:
I’m trying to make use of the Netlify stale-while-revalidate header to serve a stale version of the page and then request the cache to fill itself with fresh content and serve that on the following request.
Current behavior:
It works for about 15 to 30 seconds (varying results). And then starts fetching the fresh content again for every page, even though it should still be stale as I’m setting it to return stale content for 7 days.
The header I’m using:
Netlify-CDN-Cache-Control: public, max-age=0, stale-while-revalidate=604800
What I have tried:
-
Using a middleware.ts file where I fetch the data in an onRequest function and then pass it to Astro.locals.pageData and setting the
Netlify-CDN-Cache-Control: public, max-age=0, stale-while-revalidate=604800
on the fetch’s headers instead of the page’s. This seems to crash the build on Netlify. -
I have tried setting a _headers and netlify.toml file in conjunction with having the header set in a […slug].astro file, even though this is not supported for SSR.
-
I have tried setting the Astro response headers in the […slug].astro file, which handles every path. This seems to be the way it’s supposed to work? But it stop serving stale content after 15-30 seconds (varying).
-
I have tried fiddling with the max-age of the header, setting max-age=1 for example instead of 0. And settings s-max-age=604800 (which shouldn’t matter).
This is the minimal reproduction:
https://fascinating-otter-d9ae75.netlify.app/
And the Github repo:
https://github.com/wsbos/united-disk
Any help or advice would be appreciated