feat(nuxt): start loading indicator state with middleware (#21003)

This commit is contained in:
Quentin 2023-06-16 16:47:20 +02:00 committed by GitHub
parent bcb3d7337b
commit 7e74e7c2fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 2 deletions

View File

@ -1,5 +1,9 @@
import { computed, defineComponent, h, onBeforeUnmount, ref } from 'vue'
import { useNuxtApp } from '#app/nuxt'
import { useRouter } from '#app/composables/router'
// @ts-expect-error virtual file
import { globalMiddleware } from '#build/middleware'
export default defineComponent({
name: 'NuxtLoadingIndicator',
@ -30,10 +34,20 @@ export default defineComponent({
// Hook to app lifecycle
// TODO: Use unified loading API
const nuxtApp = useNuxtApp()
nuxtApp.hook('page:start', indicator.start)
const router = useRouter()
globalMiddleware.unshift(indicator.start)
router.beforeResolve((to, from) => {
if (to === from) {
indicator.finish()
}
})
nuxtApp.hook('page:finish', indicator.finish)
nuxtApp.hook('vue:error', indicator.finish)
onBeforeUnmount(indicator.clear)
onBeforeUnmount(() => {
globalMiddleware.splice(globalMiddleware.indexOf(indicator.start, 1))
indicator.clear()
})
return () => h('div', {
class: 'nuxt-loading-indicator',