Skip to content

Commit

Permalink
fix: keep __nextDataReq query param working
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jan 2, 2025
1 parent c3e328c commit f5bb340
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/run/handlers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions tests/e2e/edge-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
2 changes: 1 addition & 1 deletion tests/fixtures/middleware-pages/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// 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.
1 change: 1 addition & 0 deletions tests/fixtures/middleware-pages/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ if (platform === 'win32') {
}
}

/** @type {import('next').NextConfig} */
module.exports = {
trailingSlash: true,
output: 'standalone',
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/middleware-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 2 additions & 1 deletion tests/fixtures/middleware-pages/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions tests/utils/create-e2e-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () =>
Expand Down

0 comments on commit f5bb340

Please sign in to comment.