Skip to content

fix(utils): do not read a query from a "?" inside the URL hash - #12319

Open
dsavy4 wants to merge 1 commit into
facebook:mainfrom
dsavy4:fix/tourlpath-hash-question-mark
Open

fix(utils): do not read a query from a "?" inside the URL hash#12319
dsavy4 wants to merge 1 commit into
facebook:mainfrom
dsavy4:fix/tourlpath-hash-question-mark

Conversation

@dsavy4

@dsavy4 dsavy4 commented Jul 24, 2026

Copy link
Copy Markdown

Motivation

toURLPath needs to tell "no query" apart from an empty query (/foo vs /foo?). Since the WHATWG URL API reports both as url.search === '', it falls back to url.href.includes('?'). But url.href also contains the fragment, so a ? that only appears inside the hash is mistaken for an empty query.

toURLPath(new URL('https://example.com/foo#bar?baz'))
// before: { pathname: '/foo', search: '', hash: 'bar?baz' }   <- wrong
// after:  { pathname: '/foo', search: undefined, hash: 'bar?baz' }

This is not just cosmetic. serializeURLPath turns an empty search into a literal ?, so round-tripping corrupts the URL:

serializeURLPath(parseURLPath('/foo#bar?baz'))
// before: '/foo?#bar?baz'   <- spurious "?"
// after:  '/foo#bar?baz'

The fix

Only look for the query in the part of the href before the fragment:

const beforeHash = url.hash ? url.href.slice(0, -url.hash.length) : url.href;
const search = url.search ? url.search.slice(1) : beforeHash.includes('?') ? '' : undefined;

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 urlUtils in packages/docusaurus-utils. The new case covers /pathname#hash?notquery, and the existing toURLPath cases still pass.

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.
@dsavy4
dsavy4 requested review from Josh-Cena and slorber as code owners July 24, 2026 22:11
@meta-cla

meta-cla Bot commented Jul 24, 2026

Copy link
Copy Markdown

Hi @dsavy4!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@netlify

netlify Bot commented Jul 24, 2026

Copy link
Copy Markdown

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit 2a8a289
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/6a63e316a3a897000866a999
😎 Deploy Preview https://deploy-preview-12319--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dsavy4

dsavy4 commented Jul 24, 2026

Copy link
Copy Markdown
Author

Hi @dsavy4!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

Signed and accepted

@meta-cla

meta-cla Bot commented Jul 24, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label Jul 24, 2026
@meta-cla

meta-cla Bot commented Jul 24, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant