Skip to content

Commit

Permalink
fix(nuxt): clear errors after navigation (nuxt#4839)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored May 6, 2022
1 parent 3e57982 commit 27d6736
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/nuxt/src/app/components/nuxt-error-page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const stacktrace = (error.stack || '')
const statusCode = String(error.statusCode || 500)
const is404 = statusCode === '404'
const statusMessage = error.statusMessage ?? is404 ? 'Page Not Found' : 'Internal Server Error'
const statusMessage = error.statusMessage ?? (is404 ? 'Page Not Found' : 'Internal Server Error')
const description = error.message || error.toString()
const stack = process.dev && !is404 ? error.description || `<pre>${stacktrace}</pre>` : undefined
Expand Down
9 changes: 4 additions & 5 deletions packages/nuxt/src/app/plugins/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
// Resolve route
const to = getRouteFromPath(url)

if (process.client && !nuxtApp.isHydrating) {
// Clear any existing errors
await callWithNuxt(nuxtApp, clearError)
}

// Run beforeEach hooks
for (const middleware of hooks['navigate:before']) {
const result = await middleware(to, route)
Expand All @@ -128,6 +123,10 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>((nuxtApp) => {
Object.assign(route, to)
if (process.client) {
window.history[replace ? 'replaceState' : 'pushState']({}, '', url)
if (!nuxtApp.isHydrating) {
// Clear any existing errors
await callWithNuxt(nuxtApp, clearError)
}
}
// Run afterEach hooks
for (const middleware of hooks['navigate:after']) {
Expand Down
14 changes: 7 additions & 7 deletions packages/nuxt/src/pages/runtime/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { createError } from 'h3'
import { withoutBase } from 'ufo'
import NuxtPage from './page'
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, throwError, clearError, navigateTo } from '#app'
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig, throwError, clearError, navigateTo, useError } from '#app'
// @ts-ignore
import routes from '#build/routes'
// @ts-ignore
Expand Down Expand Up @@ -107,7 +107,12 @@ export default defineNuxtPlugin(async (nuxtApp) => {
named: {}
}

router.afterEach((to) => {
const error = useError()
router.afterEach(async (to) => {
if (process.client && !nuxtApp.isHydrating && error.value) {
// Clear any existing errors
await callWithNuxt(nuxtApp, clearError)
}
if (to.matched.length === 0) {
callWithNuxt(nuxtApp, throwError, [createError({
statusCode: 404,
Expand Down Expand Up @@ -147,11 +152,6 @@ export default defineNuxtPlugin(async (nuxtApp) => {
}
}

if (process.client && !nuxtApp.isHydrating) {
// Clear any existing errors
await callWithNuxt(nuxtApp, clearError)
}

for (const entry of middlewareEntries) {
const middleware = typeof entry === 'string' ? nuxtApp._middleware.named[entry] || await namedMiddleware[entry]?.().then(r => r.default || r) : entry

Expand Down

0 comments on commit 27d6736

Please sign in to comment.