fix(utils): do not read a query from a "?" inside the URL hash - #12319
fix(utils): do not read a query from a "?" inside the URL hash#12319dsavy4 wants to merge 1 commit into
Conversation
toURLPath distinguishes "no query" from an empty query by falling back to
url.href.includes("?"), but href also includes the fragment. So a "?"
inside the hash (for example "/foo#bar?baz") was read as an empty query.
That returned search: "" instead of undefined, and serializeURLPath turns
an empty search into a literal "?", so round-tripping "/foo#bar?baz" through
parseURLPath and serializeURLPath corrupted it to "/foo?#bar?baz".
Only look for the query in the part before the fragment. Added a test for a
hash that contains a question mark.
|
Hi @dsavy4! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
✅ [V2]Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Signed and accepted |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Motivation
toURLPathneeds to tell "no query" apart from an empty query (/foovs/foo?). Since the WHATWG URL API reports both asurl.search === '', it falls back tourl.href.includes('?'). Buturl.hrefalso contains the fragment, so a?that only appears inside the hash is mistaken for an empty query.This is not just cosmetic.
serializeURLPathturns an emptysearchinto a literal?, so round-tripping corrupts the URL:The fix
Only look for the query in the part of the href before the fragment:
All existing behavior is preserved (real query, empty query
/foo?, empty query before hash/foo?#, plain path, hash only). Added a regression test for a hash that contains a?.Test Plan
yarn jest urlUtilsinpackages/docusaurus-utils. The new case covers/pathname#hash?notquery, and the existingtoURLPathcases still pass.