Unverified Commit 2bc8e740 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update res.revalidate to pass through headers (#44981)

parent 09830502
...@@ -353,6 +353,15 @@ function setPreviewData<T>( ...@@ -353,6 +353,15 @@ function setPreviewData<T>(
return res return res
} }
const filteredRevalidateHeaders = new Set([
'content-type',
'content-encoding',
'accept',
'accept-language',
'accept-encoding',
'connection',
])
async function revalidate( async function revalidate(
urlPath: string, urlPath: string,
opts: { opts: {
...@@ -366,7 +375,7 @@ async function revalidate( ...@@ -366,7 +375,7 @@ async function revalidate(
`Invalid urlPath provided to revalidate(), must be a path e.g. /blog/post-1, received ${urlPath}` `Invalid urlPath provided to revalidate(), must be a path e.g. /blog/post-1, received ${urlPath}`
) )
} }
const revalidateHeaders = { const revalidateHeaders: HeadersInit = {
[PRERENDER_REVALIDATE_HEADER]: context.previewModeId, [PRERENDER_REVALIDATE_HEADER]: context.previewModeId,
...(opts.unstable_onlyGenerated ...(opts.unstable_onlyGenerated
? { ? {
...@@ -375,14 +384,17 @@ async function revalidate( ...@@ -375,14 +384,17 @@ async function revalidate(
: {}), : {}),
} }
for (const key of Object.keys(req.headers)) {
if (!filteredRevalidateHeaders.has(key) && !key.startsWith('x-vercel')) {
revalidateHeaders[key] = req.headers[key] as string
}
}
try { try {
if (context.trustHostHeader) { if (context.trustHostHeader) {
const res = await fetch(`https://${req.headers.host}${urlPath}`, { const res = await fetch(`https://${req.headers.host}${urlPath}`, {
method: 'HEAD', method: 'HEAD',
headers: { headers: revalidateHeaders,
...revalidateHeaders,
cookie: req.headers.cookie || '',
},
}) })
// we use the cache header to determine successful revalidate as // we use the cache header to determine successful revalidate as
// a non-200 status code can be returned from a successful revalidate // a non-200 status code can be returned from a successful revalidate
......
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