]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: simplify parseURL
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 6 Dec 2024 08:40:16 +0000 (09:40 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 6 Dec 2024 08:40:16 +0000 (09:40 +0100)
packages/router/src/location.ts

index e0aa540526e5bd65c52da44ae6d5bdac2e87ccd8..0811c35c265af3fa8d8a6414a69b973c8e9fc28b 100644 (file)
@@ -52,19 +52,19 @@ export function parseURL(
 
   // NOTE: we could use URL and URLSearchParams but they are 2 to 5 times slower than this method
   const hashPos = location.indexOf('#')
-  // let searchPos = location.indexOf('?')
-  let searchPos =
-    hashPos >= 0
-      ? // find the query string before the hash to avoid including a ? in the hash
-        // e.g. /foo#hash?query -> has no query
-        location.lastIndexOf('?', hashPos)
-      : location.indexOf('?')
+  let searchPos = location.indexOf('?')
+
+  // This ensures that the ? is not part of the hash
+  // e.g. /foo#hash?query -> has no query
+  searchPos = hashPos >= 0 && searchPos > hashPos ? -1 : searchPos
 
   if (searchPos >= 0) {
     path = location.slice(0, searchPos)
-    searchString =
-      '?' +
-      location.slice(searchPos + 1, hashPos > 0 ? hashPos : location.length)
+    // keep the ? char
+    searchString = location.slice(
+      searchPos,
+      hashPos > 0 ? hashPos : location.length
+    )
 
     query = parseQuery(searchString)
   }
@@ -213,7 +213,7 @@ export function resolveRelativePath(to: string, from: string): string {
     return to
   }
 
-  // resolve '' with '/anything' -> '/anything'
+  // resolve to: '' with from: '/anything' -> '/anything'
   if (!to) return from
 
   const fromSegments = from.split('/')