-
Notifications
You must be signed in to change notification settings - Fork 26.7k
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 (#40260)
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) ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md`
- Loading branch information
Showing
4 changed files
with
32 additions
and
4 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