Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure headers instance is serialized #51047

Merged
merged 1 commit into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ export class IncrementalCache {
this.fetchCacheKeyPrefix || '',
url,
init.method,
init.headers,
typeof (init.headers || {}).keys === 'function'
? Object.fromEntries(init.headers as Headers)
: init.headers,
init.mode,
init.redirect,
init.credentials,
Expand Down
23 changes: 23 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 @@ -34,6 +34,21 @@ createNextDescribe(
}
})

it('should correctly include headers instance in cache key', async () => {
const res = await next.fetch('/variable-revalidate/headers-instance')
expect(res.status).toBe(200)

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

const data1 = $('#page-data').text()
const data2 = $('#page-data2').text()
expect(data1).not.toBe(data2)

expect(data1).toBeTruthy()
expect(data2).toBeTruthy()
})

it.skip.each([
{
path: '/react-fetch-deduping-node',
Expand Down Expand Up @@ -572,6 +587,9 @@ createNextDescribe(
'variable-revalidate/encoding.html',
'variable-revalidate/encoding.rsc',
'variable-revalidate/encoding/page.js',
'variable-revalidate/headers-instance.html',
'variable-revalidate/headers-instance.rsc',
'variable-revalidate/headers-instance/page.js',
'variable-revalidate/no-store/page.js',
'variable-revalidate/post-method-request/page.js',
'variable-revalidate/post-method.html',
Expand Down Expand Up @@ -813,6 +831,11 @@ createNextDescribe(
initialRevalidateSeconds: 3,
srcRoute: '/variable-revalidate/encoding',
},
'/variable-revalidate/headers-instance': {
dataRoute: '/variable-revalidate/headers-instance.rsc',
initialRevalidateSeconds: 10,
srcRoute: '/variable-revalidate/headers-instance',
},
'/variable-revalidate/post-method': {
dataRoute: '/variable-revalidate/post-method.rsc',
initialRevalidateSeconds: 10,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}

export default async function Page() {
const data = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: new Headers({
'x-hello': 'world',
}),
next: {
revalidate: false,
},
}
).then((res) => res.text())

const data2 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: new Headers({
'x-hello': 'again',
}),
next: {
revalidate: false,
},
}
).then((res) => res.text())

return (
<>
<p id="page">/variable-revalidate/post-method-cached</p>
<p id="page-data">{data}</p>
<p id="page-data2">{data2}</p>
</>
)
}