Skip to content

Commit f5bb340

Browse files
committed
fix: keep __nextDataReq query param working
1 parent c3e328c commit f5bb340

File tree

7 files changed

+37
-3
lines changed

7 files changed

+37
-3
lines changed

src/run/handlers/server.ts

+9
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

+20
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

+1-1
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

+1
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

+3-1
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

+2-1
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

+1
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)