mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-23 11:52:38 +00:00
26 lines
810 B
TypeScript
26 lines
810 B
TypeScript
|
import { defineNuxtPlugin } from '#app/nuxt'
|
||
|
import { useRouter } from '#app/composables/router'
|
||
|
|
||
|
export default defineNuxtPlugin((nuxtApp) => {
|
||
|
const router = useRouter()
|
||
|
|
||
|
const chunkErrors = new Set()
|
||
|
|
||
|
router.beforeEach(() => { chunkErrors.clear() })
|
||
|
nuxtApp.hook('app:chunkError', ({ error }) => { chunkErrors.add(error) })
|
||
|
|
||
|
router.onError((error, to) => {
|
||
|
if (!chunkErrors.has(error)) { return }
|
||
|
|
||
|
let handledPath: Record<string, any> = {}
|
||
|
try {
|
||
|
handledPath = JSON.parse(localStorage.getItem('nuxt:reload') || '{}')
|
||
|
} catch {}
|
||
|
|
||
|
if (handledPath?.path !== to.fullPath || handledPath?.expires < Date.now()) {
|
||
|
localStorage.setItem('nuxt:reload', JSON.stringify({ path: to.fullPath, expires: Date.now() + 10000 }))
|
||
|
window.location.href = to.fullPath
|
||
|
}
|
||
|
})
|
||
|
})
|