fix(vue-app): reload page once after loading chunk error (#8978)

This commit is contained in:
rinu 2021-03-28 21:25:05 +03:00 committed by GitHub
parent 248a5c85a9
commit 3fb9194416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -146,7 +146,29 @@ export function resolveRouteComponents (route, fn) {
flatMapComponents(route, async (Component, instance, match, key) => {
// If component is a function, resolve it
if (typeof Component === 'function' && !Component.options) {
try {
Component = await Component()
} catch (error) {
// Handle webpack chunk loading errors
// This may be due to a new deployment or a network problem
if (
error &&
error.name === 'ChunkLoadError' &&
typeof window !== 'undefined' &&
window.sessionStorage
) {
const timeNow = Date.now()
const previousReloadTime = parseInt(window.sessionStorage.getItem('nuxt-reload'))
// check for previous reload time not to reload infinitely
if (!previousReloadTime || previousReloadTime + 60000 < timeNow) {
window.sessionStorage.setItem('nuxt-reload', timeNow)
window.location.reload(true /* skip cache */)
}
}
throw error
}
}
match.components[key] = Component = sanitizeComponent(Component)
return typeof fn === 'function' ? fn(Component, instance, match, key) : Component