AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Link Header Values Accumulate After Cache Components Revalidation When Next/Font Is Used With Middleware Proxy

When a Next.js application uses Cache Components with time-based revalidation, a middleware proxy that sets a Link header (e.g., alternate hreflang links) and next/font/local font preloads, each revalidation appends a new set of Link headers instead of replacing the previous set, causing unbounded header growth and eventual 502 errors.

highConfidence 75%Next.jsAffected V16.2.9Affected V16.3.0-Canary.56

Origin Analysis

During Cache Components revalidation, the merge of the cached response headers and the newly generated response headers uses append semantics for all headers. Next/font preloads are emitted as a separate Link header. When middleware sets a Link header on the rewritten response, the fresh response contains both the middleware Link header and the font preload Link header. The revalidation logic appends the fresh headers to the existing cached headers instead of replacing them, leading to accumulation of the middleware Link header on every revalidation cycle.
1. Clone https://github.com/KashapovK/next-link-header-cache-repro 2. Run `npm ci` 3. Build the Next.js app with `output: 'standalone'` 4. Start the standalone server on port 3003 5. Run `npm run reproduce` 6. Observe that the number of hreflang values in the Link header increases by 4 after each revalidation cycle.

Fixing Code Block

Edge Case Audit

This patch changes header merging semantics for all Cache Components revalidations. It may affect cases where multiple Link headers from different sources (e.g., middleware and next/font) are intentionally combined. If other code relies on append behavior, this could drop headers that are only present in the cached entry and not regenerated in the fresh response. Test thoroughly with multiple header sources. Rollback: revert the patch and restart the server to clear accumulated headers temporarily.

Ecosystem Topology