mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): stop loading indicator on navigation failure (#21751)
This commit is contained in:
parent
f28ff76559
commit
9f5130d06b
@ -37,13 +37,24 @@ export default defineComponent({
|
|||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
globalMiddleware.unshift(indicator.start)
|
globalMiddleware.unshift(indicator.start)
|
||||||
|
router.onError(() => {
|
||||||
|
indicator.finish()
|
||||||
|
})
|
||||||
router.beforeResolve((to, from) => {
|
router.beforeResolve((to, from) => {
|
||||||
if (to === from || to.matched.every((comp, index) => comp.components && comp.components?.default === from.matched[index]?.components?.default)) {
|
if (to === from || to.matched.every((comp, index) => comp.components && comp.components?.default === from.matched[index]?.components?.default)) {
|
||||||
indicator.finish()
|
indicator.finish()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.afterEach((_to, _from, failure) => {
|
||||||
|
if (failure) {
|
||||||
|
indicator.finish()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
nuxtApp.hook('page:finish', indicator.finish)
|
nuxtApp.hook('page:finish', indicator.finish)
|
||||||
nuxtApp.hook('vue:error', indicator.finish)
|
nuxtApp.hook('vue:error', indicator.finish)
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
const index = globalMiddleware.indexOf(indicator.start)
|
const index = globalMiddleware.indexOf(indicator.start)
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
|
@ -175,7 +175,10 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (result || result === false) { return result }
|
|
||||||
|
if (result || result === false) {
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -168,6 +168,15 @@ describe('pages', () => {
|
|||||||
await expectNoClientErrors('/not-found')
|
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 () => {
|
it('should render correctly when loaded on a different path', async () => {
|
||||||
const page = await createPage('/proxy')
|
const page = await createPage('/proxy')
|
||||||
|
|
||||||
|
1
test/fixtures/basic/extends/node_modules/foo/layouts/default.vue
generated
vendored
1
test/fixtures/basic/extends/node_modules/foo/layouts/default.vue
generated
vendored
@ -1,5 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
|
<NuxtLoadingIndicator id="loading-indicator" />
|
||||||
<div>Extended layout from foo</div>
|
<div>Extended layout from foo</div>
|
||||||
<NuxtPage />
|
<NuxtPage />
|
||||||
</div>
|
</div>
|
||||||
|
6
test/fixtures/basic/pages/index.vue
vendored
6
test/fixtures/basic/pages/index.vue
vendored
@ -29,6 +29,12 @@
|
|||||||
<NuxtLink to="/chunk-error" :prefetch="false">
|
<NuxtLink to="/chunk-error" :prefetch="false">
|
||||||
Chunk error
|
Chunk error
|
||||||
</NuxtLink>
|
</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 }}
|
Some value: {{ someValue }}
|
||||||
<button @click="someValue++">
|
<button @click="someValue++">
|
||||||
Increment state
|
Increment state
|
||||||
|
15
test/fixtures/basic/pages/middleware-abort-non-fatal.vue
vendored
Normal file
15
test/fixtures/basic/pages/middleware-abort-non-fatal.vue
vendored
Normal 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>
|
Loading…
Reference in New Issue
Block a user