Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[edge] fix URLSearchParams lacking data from rewrite #40260

Merged

Commits on Sep 8, 2022

  1. [edge] fix URLSearchParams lacking data from rewrite

    Given the change in vercel#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)
    Schniz committed Sep 8, 2022
    Copy the full SHA
    37e3323 View commit details
    Browse the repository at this point in the history
  2. fix test

    Schniz committed Sep 8, 2022
    Copy the full SHA
    dc37eac View commit details
    Browse the repository at this point in the history