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

fix: set x-deployment-id to every middleware prefetch request #71193

Merged
merged 9 commits into from
Dec 18, 2024
5 changes: 4 additions & 1 deletion packages/next/src/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,10 @@ function fetchNextData({
headers: Object.assign(
{} as HeadersInit,
isPrefetch ? { purpose: 'prefetch' } : {},
isPrefetch && hasMiddleware ? { 'x-middleware-prefetch': '1' } : {}
isPrefetch && hasMiddleware ? { 'x-middleware-prefetch': '1' } : {},
process.env.NEXT_DEPLOYMENT_ID
? { 'x-deployment-id': process.env.NEXT_DEPLOYMENT_ID }
: {}
),
method: params?.method ?? 'GET',
})
Expand Down
5 changes: 5 additions & 0 deletions test/production/deployment-id-handling/app/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextRequest, NextResponse } from 'next/server'

export function middleware(_req: NextRequest) {
return NextResponse.next()
}
4 changes: 4 additions & 0 deletions test/production/deployment-id-handling/app/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import testImage from '../public/test.jpg'
import Image from 'next/image'
import styles from './styles.module.css'
import Link from 'next/link'

export default function Page() {
return (
<>
<p className={styles.template}>hello pages</p>
<p id="deploymentId">{process.env.NEXT_DEPLOYMENT_ID}</p>
<Image src={testImage} alt="test image" />
<Link href="/pages-edge" id="edge-link">
Edge
</Link>

<button
onClick={() => {
Expand Down
12 changes: 10 additions & 2 deletions test/production/deployment-id-handling/app/pages/pages-edge.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import testImage from '../public/test.jpg'
import Image from 'next/image'

export default function Page() {
export default function Page({ data }: { data: string }) {
return (
<>
<p>hello pages edge</p>
<h1>{data}</h1>
<Image src={testImage} alt="test image" />
<p id="deploymentId">{process.env.NEXT_DEPLOYMENT_ID}</p>

Expand All @@ -25,3 +25,11 @@ export default function Page() {
export const config = {
runtime: 'experimental-edge',
}

export function getServerSideProps() {
return {
props: {
data: 'hello pages edge',
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ describe.each(['NEXT_DEPLOYMENT_ID', 'CUSTOM_DEPLOYMENT_ID'])(
}
)

it('should contain deployment id in prefetch request', async () => {
const dataHeaders = []
const browser = await next.browser('/', {
beforePageLoad(page) {
page.on('request', async (req) => {
const headers = await req.allHeaders()
if (headers['x-nextjs-data']) {
dataHeaders.push(headers)
}
})
},
})

await browser.elementByCss('#edge-link').click()

await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('hello pages edge')
expect(await browser.url()).toContain('/pages-edge')
expect(dataHeaders.length).toBeGreaterThan(0)
})

expect(
dataHeaders.every(
(headers) => headers['x-deployment-id'] === deploymentId
)
).toBe(true)
})

it('should contain deployment id in RSC payload request headers', async () => {
const rscHeaders = []
const browser = await next.browser('/from-app', {
Expand Down
Loading