Skip to content

Commit

Permalink
fix: don't attempt to keep old query param working
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Jan 2, 2025
1 parent f5bb340 commit 4289aa2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
16 changes: 10 additions & 6 deletions edge-runtime/lib/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
normalizeLocalePath,
normalizeTrailingSlash,
relativizeURL,
removeBasePath,
rewriteDataPath,
} from './util.ts'

export interface FetchEventResult {
Expand Down Expand Up @@ -180,14 +182,16 @@ export const buildResponse = async ({
}

if (isDataReq) {
// The rewrite target is a data request, but a middleware rewrite target is always for the page route,
// so we need to tell the server this is a data request. Setting the `x-nextjs-data` header is not enough. 🤷
rewriteUrl.searchParams.set('__nextDataReq', '1')
rewriteUrl.pathname = rewriteDataPath({
dataUrl: new URL(request.url).pathname,
newRoute: removeBasePath(rewriteUrl.pathname, nextConfig?.basePath),
basePath: nextConfig?.basePath,
})
} else {
// respect trailing slash rules to prevent 308s
rewriteUrl.pathname = normalizeTrailingSlash(rewriteUrl.pathname, nextConfig?.trailingSlash)
}

// respect trailing slash rules to prevent 308s
rewriteUrl.pathname = normalizeTrailingSlash(rewriteUrl.pathname, nextConfig?.trailingSlash)

const target = normalizeLocalizedTarget({ target: rewriteUrl.toString(), request, nextConfig })
if (target === request.url) {
logger.withFields({ rewrite_url: rewrite }).debug('Rewrite url is same as original url')
Expand Down
9 changes: 0 additions & 9 deletions src/run/handlers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ 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
11 changes: 4 additions & 7 deletions tests/e2e/edge-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,11 @@ test('it should render OpenGraph image meta tag correctly', async ({ page, middl
})

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',
},
const response = await fetch(`${middlewarePages.url}/_next/data/build-id/sha.json`, {
headers: {
'x-nextjs-data': '1',
},
)
})

expect(response.ok).toBe(true)
const body = await response.text()
Expand Down

0 comments on commit 4289aa2

Please sign in to comment.