Skip to content

Commit bb5fca6

Browse files
dsavy4slorber
andauthored
fix(utils): do not read a query from a "?" inside the URL hash (#12319)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
1 parent b5c8507 commit bb5fca6

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

packages/docusaurus-utils/src/__tests__/urlUtils.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ describe('toURLPath', () => {
277277
hash: '',
278278
});
279279
});
280+
281+
it('pathname + hash containing a question mark (no query)', () => {
282+
const url = parseURLOrPath('/pathname#hash?notquery');
283+
expect(toURLPath(url)).toEqual({
284+
pathname: '/pathname',
285+
search: undefined,
286+
hash: 'hash?notquery',
287+
});
288+
});
280289
});
281290

282291
describe('parseLocalURLPath', () => {

packages/docusaurus-utils/src/urlUtils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,17 @@ export type URLPath = {pathname: string; search?: string; hash?: string};
177177
export function toURLPath(url: URL): URLPath {
178178
const {pathname} = url;
179179

180+
// Only the part before the fragment can contain the query string. A "?"
181+
// inside the hash (e.g. "/foo#bar?baz") must not be read as an empty query.
182+
const beforeHash = url.hash ? url.href.slice(0, -url.hash.length) : url.href;
183+
180184
// Fixes annoying url.search behavior
181185
// "" => undefined
182186
// "?" => ""
183187
// "?param => "param"
184188
const search = url.search
185189
? url.search.slice(1)
186-
: url.href.includes('?')
190+
: beforeHash.includes('?')
187191
? ''
188192
: undefined;
189193

0 commit comments

Comments
 (0)