fix(nuxt): register redirect middleware before pushing route (#27016)

This commit is contained in:
Daniel Roe 2024-05-01 18:04:19 +01:00 committed by GitHub
parent 1add938a58
commit cd59dc1b8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,7 +9,7 @@ import {
createWebHistory, createWebHistory,
} from '#vue-router' } from '#vue-router'
import { createError } from 'h3' import { createError } from 'h3'
import { isEqual, isSamePath, withoutBase } from 'ufo' import { isEqual, withoutBase } from 'ufo'
import type { PageMeta } from '../composables' import type { PageMeta } from '../composables'
@ -139,6 +139,36 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
named: {}, named: {},
} }
const error = useError()
if (import.meta.client || !nuxtApp.ssrContext?.islandContext) {
router.afterEach(async (to, _from, failure) => {
delete nuxtApp._processingMiddleware
if (import.meta.client && !nuxtApp.isHydrating && error.value) {
// Clear any existing errors
await nuxtApp.runWithContext(clearError)
}
if (failure) {
await nuxtApp.callHook('page:loading:end')
}
if (import.meta.server && failure?.type === 4 /* ErrorTypes.NAVIGATION_ABORTED */) {
return
}
if (to.matched.length === 0) {
await nuxtApp.runWithContext(() => showError(createError({
statusCode: 404,
fatal: false,
statusMessage: `Page not found: ${to.fullPath}`,
data: {
path: to.fullPath,
},
})))
} else if (import.meta.server && to.redirectedFrom && to.fullPath !== initialURL) {
await nuxtApp.runWithContext(() => navigateTo(to.fullPath || '/'))
}
})
}
try { try {
if (import.meta.server) { if (import.meta.server) {
await router.push(initialURL) await router.push(initialURL)
@ -228,34 +258,6 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
await nuxtApp.callHook('page:loading:end') await nuxtApp.callHook('page:loading:end')
}) })
const error = useError()
router.afterEach(async (to, _from, failure) => {
delete nuxtApp._processingMiddleware
if (import.meta.client && !nuxtApp.isHydrating && error.value) {
// Clear any existing errors
await nuxtApp.runWithContext(clearError)
}
if (failure) {
await nuxtApp.callHook('page:loading:end')
}
if (import.meta.server && failure?.type === 4 /* ErrorTypes.NAVIGATION_ABORTED */) {
return
}
if (to.matched.length === 0) {
await nuxtApp.runWithContext(() => showError(createError({
statusCode: 404,
fatal: false,
statusMessage: `Page not found: ${to.fullPath}`,
data: {
path: to.fullPath,
},
})))
} else if (import.meta.server && to.fullPath !== initialURL && (to.redirectedFrom || !isSamePath(to.fullPath, initialURL))) {
await nuxtApp.runWithContext(() => navigateTo(to.fullPath || '/'))
}
})
nuxtApp.hooks.hookOnce('app:created', async () => { nuxtApp.hooks.hookOnce('app:created', async () => {
try { try {
// #4920, #4982 // #4920, #4982