FMHYedit/.vitepress/theme/composables/nprogress.ts

24 lines
664 B
TypeScript
Raw Normal View History

2024-01-25 16:32:45 +00:00
import nprogress, { type NProgress } from 'nprogress'
import type { EnhanceAppContext } from 'vitepress'
2023-11-17 07:56:59 +00:00
2024-01-25 16:32:45 +00:00
export function loadProgress(router: EnhanceAppContext['router']): NProgress {
if (typeof window === 'undefined') return
2023-11-17 07:56:59 +00:00
setTimeout(() => {
2024-01-25 16:32:45 +00:00
nprogress.configure({ showSpinner: false })
2023-11-17 07:56:59 +00:00
2024-01-25 16:32:45 +00:00
const cacheBeforeRouteChange = router.onBeforeRouteChange
const cacheAfterRouteChange = router.onAfterRouteChanged
2023-11-17 07:56:59 +00:00
router.onBeforeRouteChange = (to) => {
2024-01-25 16:32:45 +00:00
nprogress.start()
cacheBeforeRouteChange?.(to)
}
2023-11-17 07:56:59 +00:00
router.onAfterRouteChanged = (to) => {
2024-01-25 16:32:45 +00:00
nprogress.done()
cacheAfterRouteChange?.(to)
}
})
2023-11-17 07:56:59 +00:00
2024-01-25 16:32:45 +00:00
return nprogress
2023-11-17 07:56:59 +00:00
}