Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[edge] fix URLSearchParams lacking data from rewrite #40260

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/next/server/next-server.ts
Expand Up @@ -2049,9 +2049,7 @@ export default class NextNodeServer extends BaseServer {

// For middleware to "fetch" we must always provide an absolute URL
const isDataReq = !!params.query.__nextDataReq
const query = urlQueryToSearchParams(
Object.assign({}, getRequestMeta(params.req, '__NEXT_INIT_QUERY') || {})
).toString()
const query = urlQueryToSearchParams(params.query).toString()
const locale = params.query.__nextLocale
// Use original pathname (without `/page`) instead of appPath for url
let normalizedPathname = params.page
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/middleware-general/app/middleware.js
Expand Up @@ -47,6 +47,12 @@ export async function middleware(request) {
return NextResponse.next()
}

if (url.pathname === '/api/edge-search-params') {
const newUrl = url.clone()
newUrl.searchParams.set('foo', 'bar')
return NextResponse.rewrite(newUrl)
}

if (url.pathname === '/') {
url.pathname = '/ssg/first'
return NextResponse.rewrite(url)
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/middleware-general/app/pages/api/edge-search-params.js
@@ -0,0 +1,10 @@
import { NextResponse } from 'next/server'
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved

export const config = { runtime: 'experimental-edge' }

/**
* @param {import('next/server').NextRequest}
*/
export default (req) => {
return NextResponse.json(Object.fromEntries(req.nextUrl.searchParams))
}
16 changes: 15 additions & 1 deletion test/e2e/middleware-general/test/index.test.ts
Expand Up @@ -116,7 +116,10 @@ describe('Middleware Runtime', () => {
'ANOTHER_MIDDLEWARE_TEST',
'STRING_ENV_VAR',
],
files: ['server/edge-runtime-webpack.js', 'server/middleware.js'],
files: expect.arrayContaining([
'server/edge-runtime-webpack.js',
'server/middleware.js',
]),
name: 'middleware',
page: '/',
matchers: [{ regexp: '^/.*$' }],
Expand Down Expand Up @@ -157,6 +160,17 @@ describe('Middleware Runtime', () => {
})
}

it('passes search params with rewrites', async () => {
const response = await fetchViaHTTP(next.url, `/api/edge-search-params`, {
a: 'b',
})
await expect(response.json()).resolves.toMatchObject({
a: 'b',
// included from middleware
foo: 'bar',
})
})

it('should have init header for NextResponse.redirect', async () => {
const res = await fetchViaHTTP(
next.url,
Expand Down