From 136bf2146071d017702c3989f08d26dc5cbe5292 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 28 May 2022 00:44:11 +0200 Subject: [PATCH] fix: correctly resolve hash including a search Fix #1422 --- __tests__/location.spec.ts | 9 +++++++++ src/location.ts | 9 +++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/__tests__/location.spec.ts b/__tests__/location.spec.ts index 4846f87a..6e1ea82c 100644 --- a/__tests__/location.spec.ts +++ b/__tests__/location.spec.ts @@ -133,6 +133,15 @@ describe('parseURL', () => { }) }) + it('parses ? after the hash', () => { + expect(parseURL('/foo/#?a=one')).toEqual({ + fullPath: '/foo/#?a=one', + path: '/foo/', + hash: '#?a=one', + query: {}, + }) + }) + it('calls parseQuery', () => { const parseQuery = jest.fn() originalParseURL(parseQuery, '/?é=é&é=a') diff --git a/src/location.ts b/src/location.ts index c6e6f498..18ca5339 100644 --- a/src/location.ts +++ b/src/location.ts @@ -53,8 +53,13 @@ export function parseURL( hash = '' // Could use URL and URLSearchParams but IE 11 doesn't support it - const searchPos = location.indexOf('?') - const hashPos = location.indexOf('#', searchPos > -1 ? searchPos : 0) + // TODO: move to new URL() + const hashPos = location.indexOf('#') + let searchPos = location.indexOf('?') + // the hash appears before the search, so it's not part of the search string + if (hashPos < searchPos && hashPos >= 0) { + searchPos = -1 + } if (searchPos > -1) { path = location.slice(0, searchPos) -- 2.47.3