diff --git a/src/runtime/internal.ts b/src/runtime/internal.ts index 0a8c0a1fa..95695efea 100644 --- a/src/runtime/internal.ts +++ b/src/runtime/internal.ts @@ -420,8 +420,9 @@ export function getDomainFromLocale(localeCode: Locale, locales: LocaleObject[], } let protocol if (process.server) { - // @ts-ignore TODO: fix type error - const { req } = useRequestEvent(nuxt) + const { + node: { req } + } = useRequestEvent(nuxt) protocol = req && isHTTPS(req) ? 'https' : 'http' } else { protocol = window.location.protocol.split(':')[0] diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index ddaa990ad..94c41a805 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -12,7 +12,9 @@ import { localeHead, LocaleObject, DefaultPrefixable, - DefaultSwitchLocalePathIntercepter + DefaultSwitchLocalePathIntercepter, + getComposer, + useSwitchLocalePath } from 'vue-i18n-routing' import { navigateTo, useState } from '#imports' import { isString, isFunction, isArray, isObject } from '@intlify/shared' @@ -310,8 +312,21 @@ export function detectRedirect( } if (differentDomains || (isSSG && process.client)) { - const routePath = context.$switchLocalePath(targetLocale) || context.$localePath(route.fullPath, targetLocale) - __DEBUG__ && console.log('detectRedirect: calculate domain routePath -> ', routePath) + /** + * `$router.currentRoute` does not yet reflect the `to` value, + * when the Router middleware handler is executed. + * if `$switchLocalePath` is called, the intended path cannot be obtained, + * because it is processed by previso's route. + * so, we don't call that function, and instead, we call `useSwitchLocalePath`, + * let it be processed by the route of the router middleware. + */ + const switchLocalePath = useSwitchLocalePath({ + i18n: getComposer(context.$i18n), + route, + router: context.$router + }) + const routePath = switchLocalePath(targetLocale) + __DEBUG__ && console.log('detectRedirect: calculate domain or ssg routePath -> ', routePath) if (isString(routePath)) { redirectPath = routePath }