Skip to content

Commit 2f7fa98

Browse files
huozhiijjk
authored andcommittedAug 26, 2024
Add deployment id header for rsc payload if present (#67255)
Add deployment id for skew protection into RSC payload --------- Co-authored-by: JJ Kasper <jj@jjsweb.site>

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed
 

‎packages/next/src/client/components/router-reducer/fetch-server-response.ts

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export async function fetchServerResponse(
5656
[NEXT_ROUTER_STATE_TREE]: string
5757
[NEXT_URL]?: string
5858
[NEXT_ROUTER_PREFETCH_HEADER]?: '1'
59+
'x-deployment-id'?: string
5960
} = {
6061
// Enable flight response
6162
[RSC_HEADER]: '1',
@@ -79,6 +80,10 @@ export async function fetchServerResponse(
7980
headers[NEXT_URL] = nextUrl
8081
}
8182

83+
if (process.env.NEXT_DEPLOYMENT_ID) {
84+
headers['x-deployment-id'] = process.env.NEXT_DEPLOYMENT_ID
85+
}
86+
8287
const uniqueCacheQuery = hexHash(
8388
[
8489
headers[NEXT_ROUTER_PREFETCH_HEADER] || '0',

‎test/production/deployment-id-handling/app/app/from-app/page.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use client'
2-
import testImage from '../../public/test.jpg'
2+
3+
import Link from 'next/link'
34
import Image from 'next/image'
5+
import testImage from '../../public/test.jpg'
46

57
export default function Page() {
68
return (
@@ -19,6 +21,9 @@ export default function Page() {
1921
>
2022
click me
2123
</button>
24+
<Link id="other-app" href="/other-app">
25+
other app
26+
</Link>
2227
</>
2328
)
2429
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use client'
2+
import testImage from '../../public/test.jpg'
3+
import Image from 'next/image'
4+
5+
export default function Page() {
6+
return (
7+
<>
8+
<h1>other app</h1>
9+
<Image src={testImage} alt="test" />
10+
<p id="deploymentId">{process.env.NEXT_DEPLOYMENT_ID}</p>
11+
12+
<button
13+
onClick={() => {
14+
import('../../data').then((mod) => {
15+
console.log('loaded data', mod)
16+
})
17+
}}
18+
id="dynamic-import"
19+
>
20+
click me
21+
</button>
22+
</>
23+
)
24+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
html {
2-
font-size: 14rem;
2+
font-size: 1.4rem;
33
color: white;
44
background: black;
55
}

‎test/production/deployment-id-handling/deployment-id-handling.test.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { nextTestSetup } from 'e2e-utils'
2-
import { check } from 'next-test-utils'
2+
import { check, retry } from 'next-test-utils'
33
import { join } from 'node:path'
44

55
describe.each(['NEXT_DEPLOYMENT_ID', 'CUSTOM_DEPLOYMENT_ID'])(
@@ -51,7 +51,9 @@ describe.each(['NEXT_DEPLOYMENT_ID', 'CUSTOM_DEPLOYMENT_ID'])(
5151
const requests = []
5252

5353
browser.on('request', (req) => {
54-
requests.push(req.url())
54+
if (req.url().includes('/_next/static')) {
55+
requests.push(req.url())
56+
}
5557
})
5658

5759
await browser.elementByCss('#dynamic-import').click()
@@ -81,8 +83,37 @@ describe.each(['NEXT_DEPLOYMENT_ID', 'CUSTOM_DEPLOYMENT_ID'])(
8183
})
8284
}
8385
)
86+
87+
it('should contain deployment id in RSC payload request headers', async () => {
88+
const rscHeaders = []
89+
const browser = await next.browser('/from-app', {
90+
beforePageLoad(page) {
91+
page.on('request', async (req) => {
92+
const headers = await req.allHeaders()
93+
if (headers['rsc']) {
94+
rscHeaders.push(headers)
95+
}
96+
})
97+
},
98+
})
99+
100+
await browser.elementByCss('#other-app').click()
101+
102+
await retry(async () => {
103+
expect(await browser.elementByCss('h1').text()).toBe('other app')
104+
expect(await browser.url()).toContain('/other-app')
105+
expect(rscHeaders.length).toBeGreaterThan(0)
106+
})
107+
108+
expect(
109+
rscHeaders.every(
110+
(headers) => headers['x-deployment-id'] === deploymentId
111+
)
112+
).toBe(true)
113+
})
84114
}
85115
)
116+
86117
describe('deployment-id-handling disabled', () => {
87118
const deploymentId = Date.now() + ''
88119
const { next } = nextTestSetup({

‎test/turbopack-build-tests-manifest.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -14779,10 +14779,12 @@
1477914779
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app",
1478014780
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app/edge",
1478114781
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should append dpl query to all assets correctly for /pages-edge",
14782+
"deployment-id-handling enabled with CUSTOM_DEPLOYMENT_ID should contain deployment id in RSC payload request headers",
1478214783
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /",
1478314784
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app",
1478414785
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /from-app/edge",
14785-
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /pages-edge"
14786+
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should append dpl query to all assets correctly for /pages-edge",
14787+
"deployment-id-handling enabled with NEXT_DEPLOYMENT_ID should contain deployment id in RSC payload request headers"
1478614788
],
1478714789
"pending": [],
1478814790
"flakey": [],

0 commit comments

Comments
 (0)
Please sign in to comment.