Skip to content

Commit f6920dd

Browse files
authored
fix: switch locale path loses query parameters (#46)
* fix: switch locale path loses query parameters * chore: add explanation comment to `routeToObject` utility function
1 parent 1eabc82 commit f6920dd

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

packages/vue-i18n-routing/src/compatibles/routing.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isVue3, isRef, unref, isVue2 } from 'vue-demi'
55
import { DEFAULT_DYNAMIC_PARAMS_KEY } from '../constants'
66
import { getLocale, getLocaleRouteName, getRouteName } from '../utils'
77

8-
import { getI18nRoutingOptions, resolve } from './utils'
8+
import { getI18nRoutingOptions, resolve, routeToObject } from './utils'
99

1010
import type { RoutingProxy, PrefixableOptions, SwitchLocalePathIntercepter } from './types'
1111
import type { Strategies, I18nRoutingOptions } from '../types'
@@ -298,23 +298,24 @@ export function switchLocalePath(this: RoutingProxy, locale: Locale): string {
298298
const { switchLocalePathIntercepter, dynamicRouteParamsKey } = getI18nRoutingOptions(this.router, this)
299299

300300
// prettier-ignore
301-
const { params, ...routeCopy } = isVue3
301+
const routeValue = isVue3
302302
? (route as RouteLocationNormalizedLoaded) // for vue-router v4
303303
: isRef<Route>(route) // for vue-router v3
304304
? route.value
305305
: route
306+
const routeCopy = routeToObject(routeValue)
306307
const langSwitchParams = getLocalizableMetaFromDynamicParams(route, dynamicRouteParamsKey)[locale] || {}
307308

308309
// eslint-disable-next-line @typescript-eslint/no-explicit-any
309310
const _baseRoute: any = {
310311
name,
311312
params: {
312-
...params,
313+
...routeCopy.params,
313314
...langSwitchParams
314315
}
315316
}
316317
if (isVue2) {
317-
_baseRoute.params[0] = params.pathMatch
318+
_baseRoute.params[0] = routeCopy.params.pathMatch
318319
}
319320

320321
const baseRoute = assign({}, routeCopy, _baseRoute)

packages/vue-i18n-routing/src/compatibles/utils.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import type { RoutingProxy } from './types'
1818
import type { I18nRoutingGlobalOptions } from '../extends/router'
1919
import type { Strategies } from '../types'
2020
import type { Locale } from '@intlify/vue-i18n-bridge'
21-
import type { VueRouter, Router } from '@intlify/vue-router-bridge'
21+
import type { VueRouter, Router, Route, RouteLocationNormalizedLoaded } from '@intlify/vue-router-bridge'
2222

2323
export function getI18nRoutingOptions(
2424
router: Router | VueRouter,
@@ -58,6 +58,26 @@ function split(str: string, index: number) {
5858
return result
5959
}
6060

61+
/**
62+
* NOTE:
63+
* Nuxt route uses a proxy with getters for performance reasons (https://github.com/nuxt/nuxt/pull/21957).
64+
* Spreading will result in an empty object, so we make a copy of the route by accessing each getter property by name.
65+
*/
66+
export function routeToObject(route: Route | RouteLocationNormalizedLoaded) {
67+
const { fullPath, query, hash, name, path, params, meta, redirectedFrom, matched } = route
68+
return {
69+
fullPath,
70+
params,
71+
query,
72+
hash,
73+
name,
74+
path,
75+
meta,
76+
matched,
77+
redirectedFrom
78+
}
79+
}
80+
6181
/**
6282
* NOTE:
6383
* vue-router v4.x `router.resolve` for a non exists path will output a warning.

0 commit comments

Comments
 (0)