Unverified Commit bc4515d8 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure initialHeaders are normalized (#49643)

This makes sure we have the initial headers in the correct shape during
build.

Fixes: https://github.com/vercel/next.js/issues/49374
parent e6d50ec6
......@@ -2490,8 +2490,29 @@ export default async function build(
if (exportRouteMeta.status !== 200) {
routeMeta.initialStatus = exportRouteMeta.status
}
if (Object.keys(exportRouteMeta.headers || {}).length) {
routeMeta.initialHeaders = exportRouteMeta.headers
const exportHeaders = exportRouteMeta.headers
const headerKeys = Object.keys(exportHeaders || {})
if (exportHeaders && headerKeys.length) {
routeMeta.initialHeaders = {}
// normalize header values as initialHeaders
// must be Record<string, string>
for (const key of headerKeys) {
let value = exportHeaders[key]
if (Array.isArray(value)) {
if (key === 'set-cookie') {
value = value.join(',')
} else {
value = value[value.length - 1]
}
}
if (typeof value === 'string') {
routeMeta.initialHeaders[key] = value
}
}
}
finalPrerenderRoutes[route] = {
......
......@@ -510,6 +510,7 @@ createNextDescribe(
'route-handler/post/route.js',
'route-handler/revalidate-360-isr/route.js',
'route-handler/revalidate-360/route.js',
'route-handler/static-cookies/route.js',
'ssg-draft-mode.html',
'ssg-draft-mode.rsc',
'ssg-draft-mode/[[...route]]/page.js',
......@@ -752,6 +753,15 @@ createNextDescribe(
initialRevalidateSeconds: 10,
srcRoute: '/route-handler/revalidate-360-isr',
},
'/route-handler/static-cookies': {
dataRoute: null,
initialHeaders: {
'set-cookie': 'theme=light; Path=/,my_company=ACME; Path=/',
'x-next-cache-tags': '/route-handler/static-cookies/route',
},
initialRevalidateSeconds: false,
srcRoute: '/route-handler/static-cookies',
},
'/variable-config-revalidate/revalidate-3': {
dataRoute: '/variable-config-revalidate/revalidate-3.rsc',
initialRevalidateSeconds: 3,
......
import { NextResponse } from 'next/server'
export function GET() {
const res = new NextResponse()
res.cookies.set('theme', 'light')
res.cookies.set('my_company', 'ACME')
return res
}
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