Unverified Commit a79950dc authored by Boris Besemer's avatar Boris Besemer Committed by GitHub

Remove tracestate header from fetch cache key (#76041)

parent a8d36ddb
...@@ -354,7 +354,10 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -354,7 +354,10 @@ export class IncrementalCache implements IncrementalCacheType {
? Object.fromEntries(init.headers as Headers) ? Object.fromEntries(init.headers as Headers)
: Object.assign({}, init.headers) : Object.assign({}, init.headers)
// w3c trace context headers can break request caching and deduplication
// so we remove them from the cache key
if ('traceparent' in headers) delete headers['traceparent'] if ('traceparent' in headers) delete headers['traceparent']
if ('tracestate' in headers) delete headers['tracestate']
const cacheString = JSON.stringify([ const cacheString = JSON.stringify([
MAIN_KEY_PREFIX, MAIN_KEY_PREFIX,
......
...@@ -105,20 +105,25 @@ describe('app-dir static/dynamic handling', () => { ...@@ -105,20 +105,25 @@ describe('app-dir static/dynamic handling', () => {
expect(initialRouteData).not.toEqual(newRouteData) expect(initialRouteData).not.toEqual(newRouteData)
}) })
it('should still cache even though the `traceparent` header was different', async () => { it('should still cache even though the W3C trace context headers `traceparent` and `tracestate` were different', async () => {
const res = await next.fetch('/strip-header-traceparent') const res = await next.fetch('/strip-w3c-trace-context-headers')
expect(res.status).toBe(200) expect(res.status).toBe(200)
const html = await res.text() const html = await res.text()
const $ = cheerio.load(html) const $ = cheerio.load(html)
const data1 = $('#data1').text() const traceparent1 = $('#traceparent1').text()
const data2 = $('#data2').text() const traceparent2 = $('#traceparent2').text()
expect(data1).toBeTruthy() const tracestate1 = $('#tracestate1').text()
expect(data1).toBe(data2) const tracestate2 = $('#tracestate2').text()
expect(traceparent1).toBeTruthy()
expect(traceparent1).toBe(traceparent2)
expect(tracestate1).toBeTruthy()
expect(tracestate1).toBe(tracestate2)
const echoedHeaders = JSON.parse($('#echoedHeaders').text()) const echoedHeaders = JSON.parse($('#echoedHeaders').text())
expect(echoedHeaders.headers.traceparent).toEqual('C') expect(echoedHeaders.headers.traceparent).toEqual('A')
expect(echoedHeaders.headers.tracestate).toEqual('A')
}) })
// Runtime logs aren't queryable in deploy mode // Runtime logs aren't queryable in deploy mode
...@@ -1030,10 +1035,10 @@ describe('app-dir static/dynamic handling', () => { ...@@ -1030,10 +1035,10 @@ describe('app-dir static/dynamic handling', () => {
"static-to-dynamic-error-forced/[id]/page_client-reference-manifest.js", "static-to-dynamic-error-forced/[id]/page_client-reference-manifest.js",
"static-to-dynamic-error/[id]/page.js", "static-to-dynamic-error/[id]/page.js",
"static-to-dynamic-error/[id]/page_client-reference-manifest.js", "static-to-dynamic-error/[id]/page_client-reference-manifest.js",
"strip-header-traceparent.html", "strip-w3c-trace-context-headers.html",
"strip-header-traceparent.rsc", "strip-w3c-trace-context-headers.rsc",
"strip-header-traceparent/page.js", "strip-w3c-trace-context-headers/page.js",
"strip-header-traceparent/page_client-reference-manifest.js", "strip-w3c-trace-context-headers/page_client-reference-manifest.js",
"too-many-cache-tags/page.js", "too-many-cache-tags/page.js",
"too-many-cache-tags/page_client-reference-manifest.js", "too-many-cache-tags/page_client-reference-manifest.js",
"unstable-cache/dynamic-undefined/page.js", "unstable-cache/dynamic-undefined/page.js",
...@@ -2192,7 +2197,7 @@ describe('app-dir static/dynamic handling', () => { ...@@ -2192,7 +2197,7 @@ describe('app-dir static/dynamic handling', () => {
"initialRevalidateSeconds": false, "initialRevalidateSeconds": false,
"srcRoute": "/ssg-draft-mode/[[...route]]", "srcRoute": "/ssg-draft-mode/[[...route]]",
}, },
"/strip-header-traceparent": { "/strip-w3c-trace-context-headers": {
"allowHeader": [ "allowHeader": [
"host", "host",
"x-matched-path", "x-matched-path",
...@@ -2201,7 +2206,7 @@ describe('app-dir static/dynamic handling', () => { ...@@ -2201,7 +2206,7 @@ describe('app-dir static/dynamic handling', () => {
"x-next-revalidated-tags", "x-next-revalidated-tags",
"x-next-revalidate-tag-token", "x-next-revalidate-tag-token",
], ],
"dataRoute": "/strip-header-traceparent.rsc", "dataRoute": "/strip-w3c-trace-context-headers.rsc",
"experimentalBypassFor": [ "experimentalBypassFor": [
{ {
"key": "Next-Action", "key": "Next-Action",
...@@ -2214,7 +2219,7 @@ describe('app-dir static/dynamic handling', () => { ...@@ -2214,7 +2219,7 @@ describe('app-dir static/dynamic handling', () => {
}, },
], ],
"initialRevalidateSeconds": 50, "initialRevalidateSeconds": 50,
"srcRoute": "/strip-header-traceparent", "srcRoute": "/strip-w3c-trace-context-headers",
}, },
"/unstable-cache/fetch/no-cache": { "/unstable-cache/fetch/no-cache": {
"allowHeader": [ "allowHeader": [
......
export default async function Page() { export default async function Page() {
const data1 = await fetch( const traceparent1 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random', 'https://next-data-api-endpoint.vercel.app/api/random',
{ {
headers: { traceparent: 'A' }, headers: { traceparent: 'A' },
...@@ -7,7 +7,7 @@ export default async function Page() { ...@@ -7,7 +7,7 @@ export default async function Page() {
} }
).then((res) => res.text()) ).then((res) => res.text())
const data2 = await fetch( const traceparent2 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random', 'https://next-data-api-endpoint.vercel.app/api/random',
{ {
headers: { traceparent: 'B' }, headers: { traceparent: 'B' },
...@@ -15,18 +15,36 @@ export default async function Page() { ...@@ -15,18 +15,36 @@ export default async function Page() {
} }
).then((res) => res.text()) ).then((res) => res.text())
const tracestate1 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: { tracestate: 'B' },
next: { revalidate: 50 },
}
).then((res) => res.text())
const tracestate2 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: { tracestate: 'B' },
next: { revalidate: 50 },
}
).then((res) => res.text())
const echoedHeaders = await fetch( const echoedHeaders = await fetch(
'https://next-data-api-endpoint.vercel.app/api/echo-headers', 'https://next-data-api-endpoint.vercel.app/api/echo-headers',
{ {
headers: { traceparent: 'C' }, headers: { traceparent: 'A', tracestate: 'A' },
next: { revalidate: 50 }, next: { revalidate: 50 },
} }
).then((res) => res.text()) ).then((res) => res.text())
return ( return (
<> <>
<p id="data1">{data1}</p> <p id="traceparent1">{traceparent1}</p>
<p id="data2">{data2}</p> <p id="traceparent2">{traceparent2}</p>
<p id="tracestate1">{tracestate1}</p>
<p id="tracestate2">{tracestate2}</p>
<p id="echoedHeaders">{echoedHeaders}</p> <p id="echoedHeaders">{echoedHeaders}</p>
</> </>
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment