nextjs - 💡(How to fix) Fix Query Parameters Become Empty When Using NextResponse.rewrite to an API Route in Middleware [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#84448Fetched 2026-04-08 02:19:23
View on GitHub
Comments
1
Participants
2
Timeline
7
Reactions
0
Timeline (top)
labeled ×3closed ×1commented ×1issue_type_added ×1

Code Example

Next.js version: 15.5.3
Node.js version: v22.15.1
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

invalid link

To Reproduce

Description

When using NextResponse.rewrite in middleware.ts to redirect a request to an API route, the query parameters from the original request are not being forwarded, resulting in an empty query object in the API route. This issue occurs despite explicitly appending query parameters to the rewritten URL using request.nextUrl.searchParams.

if (request.nextUrl.pathname === "/") {
const tempUrl = new URL('/api/testing/', request.url)
tempUrl.searchParams.set('locale', request.nextUrl.locale)
return NextResponse.rewrite(tempUrl);
}

Current vs. Expected behavior

Expected Behavior:

The API route should receive the query parameters and return {"query":{"locale":"en"}}. The console log in the middleware should show the rewritten URL as http://localhost:3000/api/testing?locale=en.

Actual Behavior:

The API route returns {"query":{}}, indicating that the query parameters are empty.

The console log in the middleware shows the correct rewritten URL (http://localhost:3000/api/testing?locale=en), but the parameters are not received by the API route.

Provide environment information

Next.js version: 15.5.3
Node.js version: v22.15.1

Which area(s) are affected? (Select all that apply)

Middleware, Route Handlers

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local)

Additional context

No response

extent analysis

TL;DR

The issue can be resolved by using the NextResponse.rewrite method with the correct URL object that includes the query parameters.

Guidance

  • Verify that the request.nextUrl.searchParams are being correctly appended to the tempUrl by logging the tempUrl object before calling NextResponse.rewrite.
  • Check the Next.js documentation for any known issues or limitations with NextResponse.rewrite and query parameters in version 15.5.3.
  • Try using the URLSearchParams API to append the query parameters to the tempUrl instead of using the searchParams property.
  • Ensure that the API route is correctly handling the query parameters and that there are no issues with the route handler.

Example

const tempUrl = new URL('/api/testing/', request.url);
tempUrl.searchParams.set('locale', request.nextUrl.locale);
console.log(tempUrl); // Verify the URL is correct
return NextResponse.rewrite(tempUrl);

Notes

The issue may be specific to the version of Next.js being used (15.5.3), and upgrading to a newer version may resolve the issue. However, without further information, it is difficult to determine the root cause.

Recommendation

Apply workaround: Use the URLSearchParams API to append the query parameters to the tempUrl and verify that the API route is correctly handling the query parameters. This approach allows for a more explicit control over the query parameters and may help resolve the issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING