workersCacheCdnAdapter() removes the route's browser Cache-Control whenever Workers Cache is used
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
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:
export async function GET() {
return Response.json(
{ ok: true },
{
headers: {
"Cache-Control": "max-age=10",
"Cloudflare-CDN-Cache-Control": "max-age=3600",
},
},
);
}cd vinext && pnpm install && pnpm build && pnpm start
curl -sI http://localhost:3000/api/data | grep -i cache-controlExpected: 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
- The adapter always sets the inner browser policy to
public, max-age=0, must-revalidate:BROWSER_REVALIDATE, applied inbuildResponseHeaders. Core deletes the route'sCache-Controlbefore calling it (applyCdnResponseHeaders). - 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
- 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 innerCache-Controlinstead ofBROWSER_REVALIDATE. The edge keeps readingCloudflare-CDN-Cache-Control, which Workers Cache strips before the client sees it. To match Vercel, removes-maxageandstale-while-revalidatefrom the browser copy when the route set onlyCache-Control. - In
finalizeGatewayResponse, keep that inner browser policy when the request stage added nothing that could personalise the response (noSet-Cookie, no middleware response headers), and fall back toprivate, max-age=0, must-revalidateotherwise. 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.
Source: cloudflare/vinext