Skip to content

Commit 9dcfd42

Browse files
committed
refactor: simplify parseURL
1 parent d1dba9c commit 9dcfd42

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

packages/router/src/location.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ export function parseURL(
5252

5353
// NOTE: we could use URL and URLSearchParams but they are 2 to 5 times slower than this method
5454
const hashPos = location.indexOf('#')
55-
// let searchPos = location.indexOf('?')
56-
let searchPos =
57-
hashPos >= 0
58-
? // find the query string before the hash to avoid including a ? in the hash
59-
// e.g. /foo#hash?query -> has no query
60-
location.lastIndexOf('?', hashPos)
61-
: location.indexOf('?')
55+
let searchPos = location.indexOf('?')
56+
57+
// This ensures that the ? is not part of the hash
58+
// e.g. /foo#hash?query -> has no query
59+
searchPos = hashPos >= 0 && searchPos > hashPos ? -1 : searchPos
6260

6361
if (searchPos >= 0) {
6462
path = location.slice(0, searchPos)
65-
searchString =
66-
'?' +
67-
location.slice(searchPos + 1, hashPos > 0 ? hashPos : location.length)
63+
// keep the ? char
64+
searchString = location.slice(
65+
searchPos,
66+
hashPos > 0 ? hashPos : location.length
67+
)
6868

6969
query = parseQuery(searchString)
7070
}
@@ -213,7 +213,7 @@ export function resolveRelativePath(to: string, from: string): string {
213213
return to
214214
}
215215

216-
// resolve '' with '/anything' -> '/anything'
216+
// resolve to: '' with from: '/anything' -> '/anything'
217217
if (!to) return from
218218

219219
const fromSegments = from.split('/')

0 commit comments

Comments
 (0)