MOPC-App/src/lib/chunk-error-recovery.ts

35 lines
1.1 KiB
TypeScript

/**
* Detects ChunkLoadError (caused by stale builds or deployment mismatches)
* and auto-reloads the page once to recover.
*/
export function isChunkLoadError(error: Error): boolean {
return (
error.name === 'ChunkLoadError' ||
error.message?.includes('Loading chunk') ||
error.message?.includes('Failed to fetch dynamically imported module') ||
error.message?.includes('error loading dynamically imported module')
)
}
/**
* Attempts auto-reload recovery for ChunkLoadError.
* Uses sessionStorage to prevent infinite reload loops (max once per 30s).
* Returns true if a reload was triggered.
*/
export function attemptChunkErrorRecovery(sectionKey: string): boolean {
if (typeof window === 'undefined') return false
const reloadKey = `chunk-reload-${sectionKey}`
const lastReload = sessionStorage.getItem(reloadKey)
const now = Date.now()
// Only auto-reload if we haven't reloaded in the last 30 seconds
if (!lastReload || now - parseInt(lastReload) > 30000) {
sessionStorage.setItem(reloadKey, String(now))
window.location.reload()
return true
}
return false
}