Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5bb340

Browse filesBrowse files
committed
fix: keep __nextDataReq query param working
1 parent c3e328c commit f5bb340
Copy full SHA for f5bb340

File tree

Expand file treeCollapse file tree

7 files changed

+37
-3
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+37
-3
lines changed

‎src/run/handlers/server.ts

Copy file name to clipboardExpand all lines: src/run/handlers/server.ts
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ export default async (request: Request) => {
8383
},
8484
})
8585

86+
if (new URL(request.url).searchParams.get('__nextDataReq')) {
87+
const NEXT_REQUEST_META = Symbol.for('NextInternalRequestMeta')
88+
// @ts-expect-error NEXT_REQUEST_META doesn't exist in IncomingMessage type
89+
const meta = req[NEXT_REQUEST_META] ?? {}
90+
meta.isNextDataReq = true
91+
// @ts-expect-error NEXT_REQUEST_META doesn't exist in IncomingMessage type
92+
req[NEXT_REQUEST_META] = meta
93+
}
94+
8695
disableFaultyTransferEncodingHandling(res as unknown as ComputeJsOutgoingMessage)
8796

8897
const requestContext = getRequestContext() ?? createRequestContext()

‎tests/e2e/edge-middleware.test.ts

Copy file name to clipboardExpand all lines: tests/e2e/edge-middleware.test.ts
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,23 @@ test('it should render OpenGraph image meta tag correctly', async ({ page, middl
5252
const size = await getImageSize(Buffer.from(imageBuffer), 'png')
5353
expect([size.width, size.height]).toEqual([1200, 630])
5454
})
55+
56+
test('json data rewrite works', async ({ middlewarePages }) => {
57+
const response = await fetch(
58+
`${middlewarePages.url}/_next/data/build-id/sha.json?__nextDataReq=1`,
59+
{
60+
headers: {
61+
'x-nextjs-data': '1',
62+
},
63+
},
64+
)
65+
66+
expect(response.ok).toBe(true)
67+
const body = await response.text()
68+
69+
expect(body).toMatch(/^{"pageProps":/)
70+
71+
const data = JSON.parse(body)
72+
73+
expect(data.pageProps.message).toBeDefined()
74+
})

‎tests/fixtures/middleware-pages/next-env.d.ts

Copy file name to clipboardExpand all lines: tests/fixtures/middleware-pages/next-env.d.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/basic-features/typescript for more information.
5+
// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information.

‎tests/fixtures/middleware-pages/next.config.js

Copy file name to clipboardExpand all lines: tests/fixtures/middleware-pages/next.config.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if (platform === 'win32') {
2121
}
2222
}
2323

24+
/** @type {import('next').NextConfig} */
2425
module.exports = {
2526
trailingSlash: true,
2627
output: 'standalone',

‎tests/fixtures/middleware-pages/package.json

Copy file name to clipboardExpand all lines: tests/fixtures/middleware-pages/package.json
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"react-dom": "18.2.0"
1414
},
1515
"devDependencies": {
16-
"@types/react": "18.2.47"
16+
"@types/node": "^20.10.6",
17+
"@types/react": "18.2.47",
18+
"typescript": "^5.3.3"
1719
}
1820
}

‎tests/fixtures/middleware-pages/tsconfig.json

Copy file name to clipboardExpand all lines: tests/fixtures/middleware-pages/tsconfig.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"moduleResolution": "node",
1212
"resolveJsonModule": true,
1313
"isolatedModules": true,
14-
"jsx": "preserve"
14+
"jsx": "preserve",
15+
"target": "ES2017"
1516
},
1617
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
1718
"exclude": ["node_modules"]

‎tests/utils/create-e2e-fixture.ts

Copy file name to clipboardExpand all lines: tests/utils/create-e2e-fixture.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ export const fixtureFactories = {
325325
bun: () => createE2EFixture('simple', { packageManger: 'bun' }),
326326
middleware: () => createE2EFixture('middleware'),
327327
middlewareOg: () => createE2EFixture('middleware-og'),
328+
middlewarePages: () => createE2EFixture('middleware-pages'),
328329
pageRouter: () => createE2EFixture('page-router'),
329330
pageRouterBasePathI18n: () => createE2EFixture('page-router-base-path-i18n'),
330331
turborepo: () =>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.