#3538·vinext

workersCacheCdnAdapter() removes the route's browser Cache-Control whenever Workers Cache is used

Author: mhsnookCreated Sep 28, 2026Updated Sep 28, 2026

Summary

With workersCacheCdnAdapter(), every response admitted to Workers Cache reaches the client with Cache-Control: private, max-age=0, must-revalidate, including when a route handler sets its own browser policy. The edge still uses the route's policy, but browsers can no longer cache the response, so every page that fetches it goes back to the Worker.

On Next.js the route handler's Cache-Control is kept (build/templates/app-route.ts: "If cache control is already set on the response we don't override it"). On Vercel it reaches the client unchanged apart from s-maxage and stale-while-revalidate, and Vercel documents a three-header pattern for giving the CDN and the browser different policies (Cache-Control headers). Workers Cache supports the same split: Cloudflare-CDN-Cache-Control is "consumed by Cloudflare and stripped from the response returned to clients", while Cache-Control is "passed through to clients" (Workers Cache configuration).

Reproduction

https://github.com/mhsnook/vinext/tree/repro/route-handler-browser-cache-control/repro/route-handler-browser-cache-control

Two copies of the same app, one on vinext 1.0.0 (scaffolded with [email protected] --cdn-cache workers-cache) and one on Next.js 16.3.6. The simplest case is /api/data:

typescript
export async function GET() {
  return Response.json(
    { ok: true },
    {
      headers: {
        "Cache-Control": "max-age=10",
        "Cloudflare-CDN-Cache-Control": "max-age=3600",
      },
    },
  );
}
bash
cd vinext && pnpm install && pnpm build && pnpm start
curl -sI http://localhost:3000/api/data | grep -i cache-control

Expected: Cache-Control: max-age=10

Actual: Cache-Control: private, max-age=0, must-revalidate

The response the cached response stage hands to Workers Cache carries Cloudflare-CDN-Cache-Control: public, max-age=3600, as intended.

Other ways a route can set the policy

Route sets vinext: browser vinext: Workers Cache Next.js: browser
revalidate = 300 + Cache-Control private, max-age=0, must-revalidate public, max-age=300, stale-while-revalidate=86400 public, max-age=300, stale-while-revalidate=86400
Cache-Control only private, max-age=0, must-revalidate public, max-age=300, stale-while-revalidate=86400 public, max-age=300, stale-while-revalidate=86400
Cache-Control + CDN-Cache-Control private, max-age=0, must-revalidate public, max-age=3600 max-age=10
dynamic = "force-static" + Cache-Control private, max-age=0, must-revalidate public, max-age=300 public, max-age=300
next.config headers() rule private, max-age=0, must-revalidate public, max-age=300 public, max-age=300
proxy.ts sets Cache-Control private, max-age=0, must-revalidate public, max-age=300 public, max-age=300
dynamic = "force-dynamic" + Cache-Control public, max-age=300, stale-while-revalidate=86400 not stored public, max-age=300, stale-while-revalidate=86400
Cache-Control: private, max-age=300 no-store, must-revalidate not stored private, max-age=300

The vinext browser column is from vite preview, which runs the built Worker in workerd, and the Next.js column from next start. The Workers Cache column was read by temporarily echoing Cloudflare-CDN-Cache-Control from finalizeGatewayResponse before it is deleted.

So the only way for a route to give browsers a max-age is to opt out of Workers Cache entirely. The last row is a separate difference: on Next.js, private, max-age=300 reaches the browser as written.

Where it happens

  1. The adapter always sets the inner browser policy to public, max-age=0, must-revalidate: BROWSER_REVALIDATE, applied in buildResponseHeaders. Core deletes the route's Cache-Control before calling it (applyCdnResponseHeaders).
  2. The gateway rewrites any response that came through the shared response stage to private, max-age=0, must-revalidate: finalizeGatewayResponse.

The adapter's doc comment gives the reasons for both steps: browsers should revalidate against the edge so they always see edge-fresh content, and the gateway uses private "so personalized request-stage headers cannot enter another shared cache". Both make sense as defaults for pages. They leave no way for a route handler whose response is the same for every visitor to keep its own browser policy, even when the request stage added no headers to it.

The e2e test that covers an explicitly public API response (tests/e2e/cloudflare-workers/route-handler-draft-cache.spec.ts, "admits explicitly public Pages API responses") uses public, s-maxage=60, a shared-cache-only policy, so it doesn't cover a route that asks for a browser max-age.

Possible fix

  1. When a route handler (or Pages API route) sets its own Cache-Control, pass it to the adapter alongside the edge policy, and have the Cloudflare adapter keep it as the inner Cache-Control instead of BROWSER_REVALIDATE. The edge keeps reading Cloudflare-CDN-Cache-Control, which Workers Cache strips before the client sees it. To match Vercel, remove s-maxage and stale-while-revalidate from the browser copy when the route set only Cache-Control.
  2. In finalizeGatewayResponse, keep that inner browser policy when the request stage added nothing that could personalise the response (no Set-Cookie, no middleware response headers), and fall back to private, max-age=0, must-revalidate otherwise. The middleware case overlaps with #3318.

Is the current behaviour intended for route handlers, or would a change along these lines be welcome? I'm happy to open a PR.