diff --git a/packages/nuxt/src/app/composables/router.ts b/packages/nuxt/src/app/composables/router.ts index 84508d2ae2..504c8decb5 100644 --- a/packages/nuxt/src/app/composables/router.ts +++ b/packages/nuxt/src/app/composables/router.ts @@ -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 }