urlpattern-polyfill icon indicating copy to clipboard operation
urlpattern-polyfill copied to clipboard

Test url with query got wrong result

Open Luomusha opened this issue 2 years ago • 1 comments

    const p = new URLPattern({ pathname: "/api/Artworks/:id", baseURL: "http://localhost" })
    const u = new URL("http://localhost/api/Artworks/1?a=1")
    console.log(p.test(u))

Luomusha avatar Sep 29 '22 15:09 Luomusha

This fails to match because the baseURL sets a non-variable search param pattern of empty string. With a baseURL you need to explicitly opt-in to a wildcard search pattern. Like:

    const p = new URLPattern({ pathname: "/api/Artworks/:id", search: '*', baseURL: "http://localhost" })
    const u = new URL("http://localhost/api/Artworks/1?a=1")
    console.log(p.test(u))

Edit: I agree this is not ideal or the most ergonomic, but there are reasons it worked out this way.

wanderview avatar Sep 29 '22 15:09 wanderview

Thanks for response

Luomusha avatar Oct 17 '22 03:10 Luomusha