From 2f833f413bf7ca864a77b8190b4ed2cb1c9e8d85 Mon Sep 17 00:00:00 2001 From: Connor Roberts <32241825+murshex@users.noreply.github.com> Date: Fri, 7 Mar 2025 08:13:32 +0700 Subject: [PATCH] fix(nuxt): preserve query/hash when calling `navigateTo` with replace (#31244) --- packages/nuxt/src/app/composables/router.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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 }