fix(nuxt): stop loading indicator on navigation failure (#21751)

This commit is contained in:
Julien Huang 2023-07-03 19:14:17 +08:00 committed by GitHub
parent f28ff76559
commit 9f5130d06b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 1 deletions

View File

@ -37,13 +37,24 @@ export default defineComponent({
const router = useRouter()
globalMiddleware.unshift(indicator.start)
router.onError(() => {
indicator.finish()
})
router.beforeResolve((to, from) => {
if (to === from || to.matched.every((comp, index) => comp.components && comp.components?.default === from.matched[index]?.components?.default)) {
indicator.finish()
}
})
router.afterEach((_to, _from, failure) => {
if (failure) {
indicator.finish()
}
})
nuxtApp.hook('page:finish', indicator.finish)
nuxtApp.hook('vue:error', indicator.finish)
onBeforeUnmount(() => {
const index = globalMiddleware.indexOf(indicator.start)
if (index >= 0) {

View File

@ -175,7 +175,10 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
return false
}
}
if (result || result === false) { return result }
if (result || result === false) {
return result
}
}
}
})

View File

@ -168,6 +168,15 @@ describe('pages', () => {
await expectNoClientErrors('/not-found')
})
it('expect no loading indicator on middleware abortNavigation', async () => {
const { page } = await renderPage('/')
await page.waitForLoadState('networkidle')
await page.locator('#middleware-abort-non-fatal').click()
expect(await page.locator('#lodagin-indicator').all()).toHaveLength(0)
await page.locator('#middleware-abort-non-fatal-error').click()
expect(await page.locator('#lodagin-indicator').all()).toHaveLength(0)
})
it('should render correctly when loaded on a different path', async () => {
const page = await createPage('/proxy')

View File

@ -1,5 +1,6 @@
<template>
<div>
<NuxtLoadingIndicator id="loading-indicator" />
<div>Extended layout from foo</div>
<NuxtPage />
</div>

View File

@ -29,6 +29,12 @@
<NuxtLink to="/chunk-error" :prefetch="false">
Chunk error
</NuxtLink>
<NuxtLink id="middleware-abort-non-fatal" to="/middleware-abort-non-fatal" :prefetch="false">
Middleware abort navigation
</NuxtLink>
<NuxtLink id="middleware-abort-non-fatal-error" to="/middleware-abort-non-fatal?error=someerror" :prefetch="false">
Middleware abort navigation with error
</NuxtLink>
Some value: {{ someValue }}
<button @click="someValue++">
Increment state

View File

@ -0,0 +1,15 @@
<template>
<div>
you should not see me
</div>
</template>
<script setup lang="ts">
definePageMeta({
middleware: [
(to) => {
return abortNavigation(to.query.error ? new Error(to.query.error.toString()) : undefined)
}
]
})
</script>