fix(nuxt): preserve query/hash when calling navigateTo with replace (#31244)

This commit is contained in:
Connor Roberts 2025-03-07 08:13:32 +07:00 committed by Daniel Roe
parent 3cd43849de
commit 2f833f413b
No known key found for this signature in database
GPG Key ID: CBC814C393D93268

View File

@ -2,7 +2,7 @@ import { getCurrentInstance, hasInjectionContext, inject, onScopeDispose } from
import type { Ref } from 'vue'
import type { NavigationFailure, NavigationGuard, RouteLocationNormalized, RouteLocationRaw, Router, useRoute as _useRoute, useRouter as _useRouter } from 'vue-router'
import { sanitizeStatusCode } from 'h3'
import { hasProtocol, isScriptProtocol, joinURL, withQuery } from 'ufo'
import { hasProtocol, isScriptProtocol, joinURL, parseQuery, parseURL, withQuery } from 'ufo'
import type { PageMeta } from '../../pages/runtime/composables'
@ -151,7 +151,16 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
// Early redirect on client-side
if (import.meta.client && !isExternal && inMiddleware) {
if (options?.replace) {
return typeof to === 'string' ? { path: to, replace: true } : { ...to, replace: true }
if (typeof to === 'string') {
const { pathname, search, hash } = parseURL(to)
return {
path: pathname,
...(search && { query: parseQuery(search) }),
...(hash && { hash }),
replace: true,
}
}
return { ...to, replace: true }
}
return to
}