Skip to content

Commit

Permalink
fix: redirection loop on trailling slash (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Feb 14, 2023
1 parent 225e0b6 commit 9b0ebb4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/runtime/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export function proxyNuxt<T extends (...args: any) => 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
Expand Down
33 changes: 17 additions & 16 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context extends NuxtApp = NuxtApp>(
args: NavigateArgs,
{
Expand All @@ -381,14 +396,7 @@ export async function navigate<Context extends NuxtApp = NuxtApp>(
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) {
Expand All @@ -401,14 +409,7 @@ export async function navigate<Context extends NuxtApp = NuxtApp>(

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()
Expand Down

0 comments on commit 9b0ebb4

Please sign in to comment.