Skip to content

Commit c54fc84

Browse files
authored
fix: avoid normalizing the fullPath (#2189)
* fix: avoid normalizing the fullPath Close #2187 * test: add test
1 parent 8e5f04f commit c54fc84

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

packages/router/__tests__/router.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,18 @@ describe('Router', () => {
534534
})
535535
})
536536

537+
// https://github.com/vuejs/router/issues/2187
538+
it('keeps a consistent value on fullPath when resolving', async () => {
539+
const { router } = await newRouter()
540+
const targetLoc = '/search#/?redirect=%2F%3Fid%3D1%23%2Fabc'
541+
expect(router.resolve(targetLoc).fullPath).toBe(targetLoc)
542+
await router.push(targetLoc)
543+
expect(router.currentRoute.value.fullPath).toBe(targetLoc)
544+
await router.push('/')
545+
await router.replace(targetLoc)
546+
expect(router.currentRoute.value.fullPath).toBe(targetLoc)
547+
})
548+
537549
describe('navigation cancelled', () => {
538550
async function checkNavigationCancelledOnPush(
539551
target?: RouteLocationRaw | false

packages/router/src/router.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,19 @@ export function createRouter(options: RouterOptions): Router {
525525
// we need to run the decoding again
526526
matchedRoute.params = normalizeParams(decodeParams(matchedRoute.params))
527527

528-
const fullPath = stringifyURL(
529-
stringifyQuery,
530-
assign({}, rawLocation, {
531-
hash: encodeHash(hash),
532-
path: matchedRoute.path,
533-
})
534-
)
528+
const fullPath =
529+
// @ts-expect-error: the rawLocation doesn't normally have a fullPath
530+
// but sometimes it gets noramlized before being passed to resolve and we can
531+
// resue it to avoid encoding an unencoded path from the user in order to be closer
532+
// to the URL constructor behavior. vuejs/router#2187
533+
rawLocation.fullPath ||
534+
stringifyURL(
535+
stringifyQuery,
536+
assign({}, rawLocation, {
537+
hash: encodeHash(hash),
538+
path: matchedRoute.path,
539+
})
540+
)
535541

536542
const href = routerHistory.createHref(fullPath)
537543
if (__DEV__) {

0 commit comments

Comments
 (0)