]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix: avoid normalizing the fullPath (#2189)
authorEduardo San Martin Morote <posva@users.noreply.github.com>
Wed, 17 Apr 2024 08:20:34 +0000 (10:20 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2024 08:20:34 +0000 (10:20 +0200)
* fix: avoid normalizing the fullPath

Close vuejs/router#2187

* test: add test

packages/router/__tests__/router.spec.ts
packages/router/src/router.ts

index 691b9d9f5320cce7239b45c3675335cb58fadf7a..3d86e1743293a5913da6ac737de4749a8c9fe402 100644 (file)
@@ -534,6 +534,18 @@ describe('Router', () => {
     })
   })
 
+  // https://github.com/vuejs/router/issues/2187
+  it('keeps a consistent value on fullPath when resolving', async () => {
+    const { router } = await newRouter()
+    const targetLoc = '/search#/?redirect=%2F%3Fid%3D1%23%2Fabc'
+    expect(router.resolve(targetLoc).fullPath).toBe(targetLoc)
+    await router.push(targetLoc)
+    expect(router.currentRoute.value.fullPath).toBe(targetLoc)
+    await router.push('/')
+    await router.replace(targetLoc)
+    expect(router.currentRoute.value.fullPath).toBe(targetLoc)
+  })
+
   describe('navigation cancelled', () => {
     async function checkNavigationCancelledOnPush(
       target?: RouteLocationRaw | false
index 20ab4d1e2ec4cfcc17cd83bb7942f559737d75fd..dcf3369d67e712e97ba8b417e522b373add3617e 100644 (file)
@@ -525,13 +525,19 @@ export function createRouter(options: RouterOptions): Router {
     // we need to run the decoding again
     matchedRoute.params = normalizeParams(decodeParams(matchedRoute.params))
 
-    const fullPath = stringifyURL(
-      stringifyQuery,
-      assign({}, rawLocation, {
-        hash: encodeHash(hash),
-        path: matchedRoute.path,
-      })
-    )
+    const fullPath =
+      // @ts-expect-error: the rawLocation doesn't normally have a fullPath
+      // but sometimes it gets noramlized before being passed to resolve and we can
+      // resue it to avoid encoding an unencoded path from the user in order to be closer
+      // to the URL constructor behavior. vuejs/router#2187
+      rawLocation.fullPath ||
+      stringifyURL(
+        stringifyQuery,
+        assign({}, rawLocation, {
+          hash: encodeHash(hash),
+          path: matchedRoute.path,
+        })
+      )
 
     const href = routerHistory.createHref(fullPath)
     if (__DEV__) {