Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[edge] fix URLSearchParams lacking data from rewrite
Given the change in #40076, when a middleware rewrites into an Edge API route and changes the querystring, the changed querystring does not appear in the incoming NextRequest object in the Edge API route ```ts // middleware.ts export function middleware(req: NextRequest) { const url = req.nextUrl url.pathname = "/api/hello" url.searchParams.set("foo", "bar") return NextResponse.rewrite(url) } // pages/api/hello.ts import { NextRequest } from "next/server" export default function handler(req: NextRequest) { return NextResponse.json(req.nextUrl.searchParams.get("foo")) } export const config = { runtime: "experimental-edge" } ``` Our expectation when requesting `/api/hello` is to see `"bar"`, but instead we are getting `null` back. This commit fixes this issue by reading the given `query` instead of using the initial querystring provided with the user request (prior to rewriting)
- Loading branch information
Showing
4 changed files
with
28 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/e2e/middleware-general/app/pages/api/edge-search-params.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export const config = { runtime: 'experimental-edge' } | ||
|
||
/** | ||
* @param {import('next/server').NextRequest} | ||
*/ | ||
export default (req) => { | ||
return NextResponse.json(Object.fromEntries(req.nextUrl.searchParams)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters