Skip to content

Commit

Permalink
fix: remove traceparent from cachekey should not remove traceparent f…
Browse files Browse the repository at this point in the history
…rom original object (#64727)

### What?

I submitted PR #64499 , it got merged, but it contains a mistake.
I'm terribly sorry about this!

By removing the traceparent from the cachekey, we mistakenly removed the
header from the original object.
Causing the actual request to be executed without the traceparent
header.

### Why?

Creating a cachekey should not alter the original object.

### How?

Flip the arguments for Object.assign

---------

Co-authored-by: Jeffrey <jeffrey@jeffreyzutt.nl>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent ea0f516 commit c1ca6ac
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export class IncrementalCache implements IncrementalCacheType {
const headers =
typeof (init.headers || {}).keys === 'function'
? Object.fromEntries(init.headers as Headers)
: Object.assign(init.headers || {}, {})
: Object.assign({}, init.headers)

if ('traceparent' in headers) delete headers['traceparent']

Expand Down
36 changes: 36 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ createNextDescribe(
}
})

it('should still cache even though the `traceparent` header was different', async () => {
const res = await next.fetch('/strip-header-traceparent')
expect(res.status).toBe(200)

const html = await res.text()
const $ = cheerio.load(html)

const data1 = $('#data1').text()
const data2 = $('#data2').text()
expect(data1).toBeTruthy()
expect(data1).toBe(data2)

const echoedHeaders = JSON.parse($('#echoedHeaders').text())
expect(echoedHeaders.headers.traceparent).toEqual('C')
})

it('should warn for too many cache tags', async () => {
const res = await next.fetch('/too-many-cache-tags')
expect(res.status).toBe(200)
Expand Down Expand Up @@ -822,6 +838,10 @@ createNextDescribe(
"static-to-dynamic-error-forced/[id]/page_client-reference-manifest.js",
"static-to-dynamic-error/[id]/page.js",
"static-to-dynamic-error/[id]/page_client-reference-manifest.js",
"strip-header-traceparent.html",
"strip-header-traceparent.rsc",
"strip-header-traceparent/page.js",
"strip-header-traceparent/page_client-reference-manifest.js",
"too-many-cache-tags/page.js",
"too-many-cache-tags/page_client-reference-manifest.js",
"unstable-cache/dynamic-undefined/page.js",
Expand Down Expand Up @@ -1544,6 +1564,22 @@ createNextDescribe(
"initialRevalidateSeconds": false,
"srcRoute": "/ssg-draft-mode/[[...route]]",
},
"/strip-header-traceparent": {
"dataRoute": "/strip-header-traceparent.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data;.*",
},
],
"initialRevalidateSeconds": 50,
"srcRoute": "/strip-header-traceparent",
},
"/variable-config-revalidate/revalidate-3": {
"dataRoute": "/variable-config-revalidate/revalidate-3.rsc",
"experimentalBypassFor": [
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/app-dir/app-static/app/strip-header-traceparent/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default async function Page() {
const data1 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: { traceparent: 'A' },
next: { revalidate: 50 },
}
).then((res) => res.text())

const data2 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: { traceparent: 'B' },
next: { revalidate: 50 },
}
).then((res) => res.text())

const echoedHeaders = await fetch(
'https://next-data-api-endpoint.vercel.app/api/echo-headers',
{
headers: { traceparent: 'C' },
next: { revalidate: 50 },
}
).then((res) => res.text())

return (
<>
<p id="data1">{data1}</p>
<p id="data2">{data2}</p>
<p id="echoedHeaders">{echoedHeaders}</p>
</>
)
}

0 comments on commit c1ca6ac

Please sign in to comment.