Skip to content

Commit

Permalink
fix: avoid normalizing the fullPath (#2189)
Browse files Browse the repository at this point in the history
* fix: avoid normalizing the fullPath

Close #2187

* test: add test
  • Loading branch information
posva committed Apr 17, 2024
1 parent 8e5f04f commit c54fc84
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
12 changes: 12 additions & 0 deletions packages/router/__tests__/router.spec.ts
Expand Up @@ -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
Expand Down
20 changes: 13 additions & 7 deletions packages/router/src/router.ts
Expand Up @@ -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__) {
Expand Down

0 comments on commit c54fc84

Please sign in to comment.