Skip to content

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

Description

@Vibeesarma

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions