fix(nuxt): ensure url is not empty string (#4781)

This commit is contained in:
pooya parsa 2022-05-05 22:46:54 +02:00 committed by GitHub
parent 5b63ae8ad5
commit ebc27ce997
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -56,6 +56,9 @@ export interface NavigateToOptions {
}
export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}): Promise<void | NavigationFailure> | RouteLocationRaw => {
if (!to) {
to = '/'
}
if (isProcessingMiddleware()) {
return to
}
@ -63,7 +66,7 @@ export const navigateTo = (to: RouteLocationRaw, options: NavigateToOptions = {}
if (process.server) {
const nuxtApp = useNuxtApp()
if (nuxtApp.ssrContext && nuxtApp.ssrContext.event) {
const redirectLocation = router.resolve(to).fullPath
const redirectLocation = router.resolve(to).fullPath || '/'
return nuxtApp.callHook('app:redirected').then(() => sendRedirect(nuxtApp.ssrContext.event, redirectLocation, options.redirectCode || 301))
}
}

View File

@ -176,8 +176,9 @@ export default defineNuxtPlugin(async (nuxtApp) => {
delete nuxtApp._processingMiddleware
if (process.server) {
if (to.fullPath !== initialURL) {
await callWithNuxt(nuxtApp, navigateTo, [to.fullPath])
const currentURL = to.fullPath || '/'
if (currentURL !== initialURL) {
await callWithNuxt(nuxtApp, navigateTo, [currentURL])
}
}
})