Skip to content

Commit 7be48db

Browse files
amannnztannersamcx
authored
fix: Merge link header from middleware with the ones from React (#73431)
Fixes #69000 <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> --------- Co-authored-by: Zack Tanner <1939140+ztanner@users.noreply.github.com> Co-authored-by: Sam Ko <sam@vercel.com>
1 parent 9bc919a commit 7be48db

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

packages/next/src/server/app-render/app-render.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,7 @@ async function renderToStream(
17351735
let reactServerResult: null | ReactServerResult = null
17361736

17371737
const setHeader = res.setHeader.bind(res)
1738+
const appendHeader = res.appendHeader.bind(res)
17381739

17391740
try {
17401741
if (
@@ -1904,7 +1905,7 @@ async function renderToStream(
19041905
nonce: ctx.nonce,
19051906
onHeaders: (headers: Headers) => {
19061907
headers.forEach((value, key) => {
1907-
setHeader(key, value)
1908+
appendHeader(key, value)
19081909
})
19091910
},
19101911
maxHeadersLength: renderOpts.reactMaxHeadersLength,
@@ -2567,14 +2568,25 @@ async function prerenderToStream(
25672568
| null
25682569
| ReactServerPrerenderResult
25692570
| ServerPrerenderStreamResult = null
2570-
const setHeader = (name: string, value: string | string[]) => {
2571-
res.setHeader(name, value)
2572-
2571+
const setMetadataHeader = (name: string) => {
25732572
metadata.headers ??= {}
25742573
metadata.headers[name] = res.getHeader(name)
2575-
2574+
}
2575+
const setHeader = (name: string, value: string | string[]) => {
2576+
res.setHeader(name, value)
2577+
setMetadataHeader(name)
25762578
return res
25772579
}
2580+
const appendHeader = (name: string, value: string | string[]) => {
2581+
if (Array.isArray(value)) {
2582+
value.forEach((item) => {
2583+
res.appendHeader(name, item)
2584+
})
2585+
} else {
2586+
res.appendHeader(name, value)
2587+
}
2588+
setMetadataHeader(name)
2589+
}
25782590

25792591
let prerenderStore: PrerenderStore | null = null
25802592

@@ -2923,7 +2935,7 @@ async function prerenderToStream(
29232935
},
29242936
onHeaders: (headers: Headers) => {
29252937
headers.forEach((value, key) => {
2926-
setHeader(key, value)
2938+
appendHeader(key, value)
29272939
})
29282940
},
29292941
maxHeadersLength: renderOpts.reactMaxHeadersLength,
@@ -3543,7 +3555,7 @@ async function prerenderToStream(
35433555
onError: htmlRendererErrorHandler,
35443556
onHeaders: (headers: Headers) => {
35453557
headers.forEach((value, key) => {
3546-
setHeader(key, value)
3558+
appendHeader(key, value)
35473559
})
35483560
},
35493561
maxHeadersLength: renderOpts.reactMaxHeadersLength,

test/e2e/app-dir/app-middleware/app-middleware.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ describe('app-dir with middleware', () => {
123123
})
124124
})
125125

126+
it('retains a link response header from the middleware', async () => {
127+
const res = await next.fetch('/preloads')
128+
expect(res.headers.get('link')).toContain(
129+
'<https://example.com/page>; rel="alternate"; hreflang="en"'
130+
)
131+
})
132+
126133
it('should be possible to modify cookies & read them in an RSC in a single request', async () => {
127134
const browser = await next.browser('/rsc-cookies')
128135

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Image from 'next/image'
2+
import { Inter } from 'next/font/google'
3+
4+
const inter = Inter({ subsets: ['latin'] })
5+
6+
export default function Page() {
7+
return (
8+
<>
9+
<Image priority src="/favicon.ico" alt="favicon" width={16} height={16} />
10+
<h1 className={inter.className}>Hello World</h1>
11+
</>
12+
)
13+
}

test/e2e/app-dir/app-middleware/middleware.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ export async function middleware(request) {
6969
return res
7070
}
7171

72+
if (request.nextUrl.pathname === '/preloads') {
73+
const res = NextResponse.next({
74+
headers: {
75+
link: '<https://example.com/page>; rel="alternate"; hreflang="en"',
76+
},
77+
})
78+
return res
79+
}
80+
7281
return NextResponse.next({
7382
request: {
7483
headers: headersFromRequest,

0 commit comments

Comments
 (0)