From 9b0ebb40b9f30b0b71ac584741e4630bc660ab19 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Wed, 15 Feb 2023 01:49:09 +0900 Subject: [PATCH] fix: redirection loop on trailling slash (#1858) --- src/runtime/internal.ts | 3 +-- src/runtime/utils.ts | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/runtime/internal.ts b/src/runtime/internal.ts index 83d4edf58..a6c1f870c 100644 --- a/src/runtime/internal.ts +++ b/src/runtime/internal.ts @@ -70,8 +70,7 @@ export function proxyNuxt any>(nuxt: NuxtApp, target switchLocalePath: nuxt.$switchLocalePath, localeHead: nuxt.$localeHead, route: (nuxt as any).$router.currentRoute.value, - router: (nuxt as any).$router, - store: undefined + router: (nuxt as any).$router }, // eslint-disable-next-line prefer-rest-params arguments diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 9be8b8e27..7e8ca7e2d 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -356,6 +356,21 @@ type NavigateArgs = { route: Route | RouteLocationNormalized | RouteLocationNormalizedLoaded } +function _navigate(redirectPath: string, status: number) { + if (isSSG && process.client) { + let pathname = window.location.pathname + if (pathname.length > 1 && pathname.endsWith('/')) { + pathname = pathname.slice(0, -1) + } + if (pathname !== redirectPath) { + window.location.assign(redirectPath) + } + return + } else { + return navigateTo(redirectPath, { redirectCode: status }) + } +} + export async function navigate( args: NavigateArgs, { @@ -381,14 +396,7 @@ export async function navigate( status = rootRedirect.statusCode } __DEBUG__ && console.log('navigate: rootRedirect mode redirectPath -> ', redirectPath, ' status -> ', status) - if (isSSG && process.client) { - if (window.location.pathname !== redirectPath) { - window.location.assign(redirectPath) - } - return - } else { - return navigateTo(redirectPath, { redirectCode: status }) - } + return _navigate(redirectPath, status) } if (process.client && skipSettingLocaleOnNavigate) { @@ -401,14 +409,7 @@ export async function navigate( if (!differentDomains) { if (redirectPath) { - if (isSSG && process.client) { - if (window.location.pathname !== redirectPath) { - window.location.assign(redirectPath) - } - return - } else { - return navigateTo(redirectPath, { redirectCode: status }) - } + return _navigate(redirectPath, status) } } else { const state = useRedirectState()