vercel / vercel/platforms

Query Parameters Lost After Middleware Rewrite for Subdomain Routing in Next.js App Router

Open
#462 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
6.7k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

When accessing a subdomain route such as
http://domain.localhost:3001/thank-you?card_id=order_XXXXXXX
the middleware rewrites the path to /s/vibees-sarma/thank-you, but the query parameters (e.g., card_id) are not preserved in the rewritten request. As a result, searchParams in the corresponding page component is always empty.

Steps to Reproduce:

Visit a subdomain route with query parameters, e.g.
http://domain.localhost:3001/thank-you?card_id=order_XXXXXXXX
The middleware rewrites the path to /s/vibees-sarma/thank-you.
In the page component, props.searchParams is {}.
Expected Behavior:
The query parameters should be preserved after the rewrite, so props.searchParams contains the expected values.

Actual Behavior:
props.searchParams is always empty after the rewrite.

Relevant Code:

// middleware.ts excerpt
if (subdomain) {
  // For other paths on subdomain (like /thank-you)
  const url = new URL(request.url);
  url.pathname = `/s/${subdomain}${pathname}`;
  return NextResponse.rewrite(url);
}

Suggested Fix:
Ensure that the rewritten URL preserves the original query parameters by constructing the new URL using the original request.url and only changing the pathname. This way, the query string is retained and available in the page component.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in middleware.ts and reproduce the subdomain request at /thank-you?card_id=order_XXXXXXX. Check the rewritten URL and the corresponding page component's searchParams; done means the original query parameters remain available after the rewrite.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript
Domain
web-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.