fix(deploy): auto-recover from stale lazy-chunk hash after redeploys#1167
Open
plind-junior wants to merge 2 commits into
Open
fix(deploy): auto-recover from stale lazy-chunk hash after redeploys#1167plind-junior wants to merge 2 commits into
plind-junior wants to merge 2 commits into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Users who had a long-lived tab open from before a production deploy were hitting "Failed to fetch dynamically imported module: …/assets/WatchlistPage-.js" and an unrecoverable ErrorBoundary screen when navigating to a lazy route. Root cause: Vite emits hashed chunk filenames; the old entry chunk in their cached tab references the old hashes, but the server only has the new hashes after the redeploy, so
import()404s.This PR wraps
React.lazy()so that on a chunk-load failure it triggers a singlewindow.location.reload(). The reload re-fetchesindex.html, which now references the current chunk hashes — the import succeeds and the user lands on the page they tried to navigate to.Root cause
WatchlistPage-C10WDGR6.js.index-<hash>.jsliterally embeds the string"WatchlistPage-C10WDGR6.js"as the dynamic-import URL.WatchlistPage-NewHash.js. The old chunk filenames are deleted by the deploy./watchlist, that old code callsimport('/assets/WatchlistPage-C10WDGR6.js')→ 404 → import rejects → ErrorBoundary catches it.Direct evidence from the production console screenshot:
Fix
src/routes.tsx— introducelazyWithReload(factory)and use it in place ofReact.lazyfor every page:Every route page (HomePage, DashboardPage, IssuesPage, … 14 total) now uses
lazyWithReload(...)instead ofReact.lazy(...). No other call sites needed to change.Behavior
Loop guard
sessionStorage[gt:chunk-reloaded]flag prevents an infinite reload loop. It's:sessionStorage(notlocalStorage) so it resets when the user closes the tab — closing & reopening a tab after a deploy works as expectedTest plan
npm run build && npm run preview— site still loads, all pages still navigate normally (regression check on the happy path).npm run build && npm run preview— note the chunk filenames indist/assets/.dist/assets/WatchlistPage-*.jsto something else (simulating "this chunk no longer exists").gt:chunk-reloadedflag toggles as expected.npm run type-checkandnpm run lintpass.Out of scope
import()calls outside route-level lazy loading (e.g. dynamic icon loading). If any otherimport()calls exist, they'd need similar wrappers. None are known to be called by user actions in this codebase, but worth checking on follow-up audits.index.html— orthogonal hardening (ensures staleindex.htmldoesn't cache indefinitely). Could be a follow-up.References
Before