Skip to content

Commit

Permalink
Ensure initialHeaders are normalized (#49643)
Browse files Browse the repository at this point in the history
This makes sure we have the initial headers in the correct shape during
build.

Fixes: #49374
  • Loading branch information
ijjk committed May 11, 2023
1 parent e6d50ec commit bc4515d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
10 changes: 10 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 @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
}

0 comments on commit bc4515d

Please sign in to comment.