-
Notifications
You must be signed in to change notification settings - Fork 21
fix: recover from stale Vite chunk load failures #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danshapiro
merged 5 commits into
main
from
replacement/chunk-error-recovery-main-20260518
May 18, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a81d30
feat: add withChunkErrorRecovery utility with broad chunk-error detec…
c3182fe
feat: add global vite:preloadError and unhandledrejection listener fo…
b5692de
fix: wrap all 5 lazy imports with chunk-error recovery
8e00d1b
fix: reload page on chunk-load errors in ErrorBoundary Try Again with…
d3786f3
refactor: export shouldReload, add try/catch on sessionStorage, make …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| const CHUNK_ERROR_RE = | ||
| /(?:failed to fetch|error loading).*dynamically imported module|importing a module script|loading chunk \d+ failed/i | ||
|
|
||
| export const RELOAD_KEY = 'freshell.chunk-reload' | ||
| const RELOAD_COOLDOWN_MS = 10_000 | ||
|
|
||
| export function isChunkLoadError(err: unknown): boolean { | ||
| return err instanceof TypeError && CHUNK_ERROR_RE.test(err.message) | ||
| } | ||
|
|
||
| export function shouldReload(): boolean { | ||
| try { | ||
| const last = sessionStorage.getItem(RELOAD_KEY) | ||
| if (last && Date.now() - parseInt(last, 10) < RELOAD_COOLDOWN_MS) { | ||
| return false | ||
| } | ||
| sessionStorage.setItem(RELOAD_KEY, String(Date.now())) | ||
| return true | ||
| } catch { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| export function withChunkErrorRecovery<T>(importPromise: Promise<T>): Promise<T> { | ||
| return importPromise.catch((err: unknown) => { | ||
| if (isChunkLoadError(err)) { | ||
| if (shouldReload()) { | ||
| window.location.reload() | ||
| return new Promise<never>(() => {}) | ||
| } | ||
| throw err | ||
| } | ||
| throw err | ||
| }) | ||
| } | ||
|
|
||
| let recoveryInitialized = false | ||
|
|
||
| export function initChunkErrorRecovery(): void { | ||
| if (recoveryInitialized) return | ||
| recoveryInitialized = true | ||
|
|
||
| window.addEventListener('vite:preloadError', (event) => { | ||
| if (shouldReload()) { | ||
| event.preventDefault() | ||
| window.location.reload() | ||
| } | ||
| }) | ||
|
|
||
| window.addEventListener('unhandledrejection', (event) => { | ||
| if (isChunkLoadError(event.reason) && shouldReload()) { | ||
| event.preventDefault() | ||
| window.location.reload() | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' | ||
| import { initChunkErrorRecovery } from '@/lib/import-retry' | ||
|
|
||
| function createRejectionEvent(reason: unknown): Event { | ||
| const event = new Event('unhandledrejection', { cancelable: true }) | ||
| Object.defineProperty(event, 'reason', { value: reason, writable: false }) | ||
| return event | ||
| } | ||
|
|
||
| describe('initChunkErrorRecovery', () => { | ||
| const originalReload = window.location.reload | ||
|
|
||
| beforeEach(() => { | ||
| Object.defineProperty(window, 'location', { | ||
| value: { ...window.location, reload: vi.fn() }, | ||
| writable: true, | ||
| configurable: true, | ||
| }) | ||
| sessionStorage.clear() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(window, 'location', { | ||
| value: { ...window.location, reload: originalReload }, | ||
| writable: true, | ||
| configurable: true, | ||
| }) | ||
| }) | ||
|
|
||
| it('reloads on vite:preloadError event', () => { | ||
| initChunkErrorRecovery() | ||
| const event = new Event('vite:preloadError', { cancelable: true }) | ||
| window.dispatchEvent(event) | ||
| expect(window.location.reload).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('reloads on unhandledrejection with chunk-load error', () => { | ||
| initChunkErrorRecovery() | ||
| const err = new TypeError( | ||
| 'Failed to fetch dynamically imported module: http://localhost/assets/chunk-abc123.js' | ||
| ) | ||
| window.dispatchEvent(createRejectionEvent(err)) | ||
| expect(window.location.reload).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not reload on unhandledrejection with non-chunk error', () => { | ||
| initChunkErrorRecovery() | ||
| const err = new Error('Something else') | ||
| window.dispatchEvent(createRejectionEvent(err)) | ||
| expect(window.location.reload).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('respects circuit breaker on vite:preloadError', () => { | ||
| sessionStorage.setItem('freshell.chunk-reload', String(Date.now())) | ||
| initChunkErrorRecovery() | ||
| const event = new Event('vite:preloadError', { cancelable: true }) | ||
| window.dispatchEvent(event) | ||
| expect(window.location.reload).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('is idempotent — calling initChunkErrorRecovery multiple times does not double-fire', () => { | ||
| initChunkErrorRecovery() | ||
| initChunkErrorRecovery() | ||
| const event = new Event('vite:preloadError', { cancelable: true }) | ||
| window.dispatchEvent(event) | ||
| expect(window.location.reload).toHaveBeenCalledTimes(1) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
sessionStorageaccess throws (for example, users with storage/cookies disabled),shouldReload()currently returnstrueunconditionally, so every chunk-load failure triggers anotherwindow.location.reload()with no working cooldown. In that environment, a stale-chunk condition can trap the app in repeated reloads and make it unusable; the fallback path should use a non-persistent in-memory breaker or returnfalseafter the first attempted reload instead of always allowing reload.Useful? React with 👍 / 👎.