Query Parameters Sorted Alphabetically by Netlify Middleware

Netlify Site Name: rusmtest.netlify.app

Issue Description:
Netlify middleware automatically sorts query parameters in alphabetical order when processing a request, even if the parameters are in a different order in the URL. This behavior causes issues in applications where the order of query parameters is critical, particularly for redirects, as many use strict regex matching that relies on the exact query string structure.

For example:

  • URL in address bar: /page?b=2&a=1
  • Received query string in middleware: /page?a=1&b=2

This impacts:

  1. Redirects: Rules fail when the query string order is altered.
  2. APIs and Business Logic: Query validation or logic tied to parameter sequence breaks when the order changes.

Steps to Reproduce

  1. Use the following middleware in a Next.js project:
export function middleware(request) {
    console.log(request.nextUrl.search); // Logs the query string
    return NextResponse.next();
}
  1. Deploy the app to Netlify.
  2. Visit: /page?b=2&a=1 in your browser.
  3. Observe that request.nextUrl.search outputs ?a=1&b=2 instead of the expected ?b=2&a=1.

Expected Behavior

The query parameters in request.nextUrl.search should retain the order they are passed in the URL.

Actual Behavior

The query parameters are reordered alphabetically by Netlify middleware.

Custom Domain: N/A (Using Netlify default domain).

Did You Use Ask Netlify?
Yes, I used Ask Netlify but couldn’t find a solution specific to preserving query parameter order in middleware.

Request:
Please confirm if this is a known issue with Netlify’s middleware or a limitation. Are there any workarounds or planned fixes to preserve the original query parameter order?

Duplicate: Don't reorder request parameters in Edge Functions