From f5bb340270f656069efb84fe2abc3b7a91f37844 Mon Sep 17 00:00:00 2001 From: pieh Date: Fri, 27 Dec 2024 18:18:15 +0100 Subject: [PATCH] fix: keep __nextDataReq query param working --- src/run/handlers/server.ts | 9 +++++++++ tests/e2e/edge-middleware.test.ts | 20 +++++++++++++++++++ tests/fixtures/middleware-pages/next-env.d.ts | 2 +- .../fixtures/middleware-pages/next.config.js | 1 + tests/fixtures/middleware-pages/package.json | 4 +++- tests/fixtures/middleware-pages/tsconfig.json | 3 ++- tests/utils/create-e2e-fixture.ts | 1 + 7 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/run/handlers/server.ts b/src/run/handlers/server.ts index 436411c812..51cfb0290d 100644 --- a/src/run/handlers/server.ts +++ b/src/run/handlers/server.ts @@ -83,6 +83,15 @@ export default async (request: Request) => { }, }) + if (new URL(request.url).searchParams.get('__nextDataReq')) { + const NEXT_REQUEST_META = Symbol.for('NextInternalRequestMeta') + // @ts-expect-error NEXT_REQUEST_META doesn't exist in IncomingMessage type + const meta = req[NEXT_REQUEST_META] ?? {} + meta.isNextDataReq = true + // @ts-expect-error NEXT_REQUEST_META doesn't exist in IncomingMessage type + req[NEXT_REQUEST_META] = meta + } + disableFaultyTransferEncodingHandling(res as unknown as ComputeJsOutgoingMessage) const requestContext = getRequestContext() ?? createRequestContext() diff --git a/tests/e2e/edge-middleware.test.ts b/tests/e2e/edge-middleware.test.ts index 0eda11b710..998eb8aff4 100644 --- a/tests/e2e/edge-middleware.test.ts +++ b/tests/e2e/edge-middleware.test.ts @@ -52,3 +52,23 @@ test('it should render OpenGraph image meta tag correctly', async ({ page, middl const size = await getImageSize(Buffer.from(imageBuffer), 'png') expect([size.width, size.height]).toEqual([1200, 630]) }) + +test('json data rewrite works', async ({ middlewarePages }) => { + const response = await fetch( + `${middlewarePages.url}/_next/data/build-id/sha.json?__nextDataReq=1`, + { + headers: { + 'x-nextjs-data': '1', + }, + }, + ) + + expect(response.ok).toBe(true) + const body = await response.text() + + expect(body).toMatch(/^{"pageProps":/) + + const data = JSON.parse(body) + + expect(data.pageProps.message).toBeDefined() +}) diff --git a/tests/fixtures/middleware-pages/next-env.d.ts b/tests/fixtures/middleware-pages/next-env.d.ts index 4f11a03dc6..52e831b434 100644 --- a/tests/fixtures/middleware-pages/next-env.d.ts +++ b/tests/fixtures/middleware-pages/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/basic-features/typescript for more information. +// see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. diff --git a/tests/fixtures/middleware-pages/next.config.js b/tests/fixtures/middleware-pages/next.config.js index 3cc07b88d1..c69795688a 100644 --- a/tests/fixtures/middleware-pages/next.config.js +++ b/tests/fixtures/middleware-pages/next.config.js @@ -21,6 +21,7 @@ if (platform === 'win32') { } } +/** @type {import('next').NextConfig} */ module.exports = { trailingSlash: true, output: 'standalone', diff --git a/tests/fixtures/middleware-pages/package.json b/tests/fixtures/middleware-pages/package.json index 5708c88b50..4f57aa1121 100644 --- a/tests/fixtures/middleware-pages/package.json +++ b/tests/fixtures/middleware-pages/package.json @@ -13,6 +13,8 @@ "react-dom": "18.2.0" }, "devDependencies": { - "@types/react": "18.2.47" + "@types/node": "^20.10.6", + "@types/react": "18.2.47", + "typescript": "^5.3.3" } } diff --git a/tests/fixtures/middleware-pages/tsconfig.json b/tests/fixtures/middleware-pages/tsconfig.json index 1c4f693a99..d88efa188d 100644 --- a/tests/fixtures/middleware-pages/tsconfig.json +++ b/tests/fixtures/middleware-pages/tsconfig.json @@ -11,7 +11,8 @@ "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, - "jsx": "preserve" + "jsx": "preserve", + "target": "ES2017" }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], "exclude": ["node_modules"] diff --git a/tests/utils/create-e2e-fixture.ts b/tests/utils/create-e2e-fixture.ts index 097bd4ac0d..31cf3496e1 100644 --- a/tests/utils/create-e2e-fixture.ts +++ b/tests/utils/create-e2e-fixture.ts @@ -325,6 +325,7 @@ export const fixtureFactories = { bun: () => createE2EFixture('simple', { packageManger: 'bun' }), middleware: () => createE2EFixture('middleware'), middlewareOg: () => createE2EFixture('middleware-og'), + middlewarePages: () => createE2EFixture('middleware-pages'), pageRouter: () => createE2EFixture('page-router'), pageRouterBasePathI18n: () => createE2EFixture('page-router-base-path-i18n'), turborepo: () =>