I run my site (the same one I wrote about in my brick and mortar business stack posts) behind a CDN. Every page request goes through it before hitting my backend (TanStack Start on Lambda). On a content site, most of those requests return the same HTML. It felt wasteful to have the Lambda wake up for every single one. The obvious fix is to set a long cache header. Cache-Control: public, max-age=86400, s-maxage=86400. The CDN serves the cached version for a day, the Lambda stays cold, everyone wins. But there is a catch. When the cache expires at hour 25, the next visitor triggers a fetch. They wait. On a Lambda with a cold start, that wait is noticeable. Not terrible, but enough that I did not want to ship it. This is where stale-while-revalidate comes in. The header looks like this: Cache-Control: public, max-age=3600, s-maxage=86400, stale-while-revalidate=86400 What it does: The CDN caches the response for 24 hours (s-maxage=86400). The browser caches it for 1 hour (max-age=3600). For another 24 hours after expiry, the CDN can serve the stale version instantly while it fetches a fresh copy in the background (stale-while-revalidate=86400). So the timeline for a reader visiting my...
← All tags