r/reactjs • u/AffectionateDealer62 • Oct 18 '26
Resource How do you handle React.lazy failures caused by stale chunks after a deployment?
A common production failure happens when a user leaves a React application open during a deployment.
The old tab may still be running build A, while the server is now serving build B. When the user later navigates to a lazy-loaded route, the browser requests a hashed chunk from build A that is no longer available.
For example:
Reports.a1b2.js // requested by the old tab
Reports.c3d4.js // available after deployment
The result can be:
ChunkLoadErrorFailed to fetch dynamically imported module- A route that works in a fresh tab but fails in an old tab
A normal React Error Boundary can display the failure, but it cannot update the old tab’s JavaScript references. Retrying the same import may also request the same missing hashed file again.
I built and published a small utility called react-lazy-chunk-recover to experiment with this problem.
The recovery approach:
- Detects likely chunk-load failures
- Optionally retries transient failures
- Allows one guarded full-page reload
- Uses
sessionStorageto prevent an infinite reload loop - Passes the error to the existing Error Boundary if recovery fails
- Avoids browser reload and storage access during SSR
Example:
import { lazyWithRetry } from "react-lazy-chunk-recover";
const Reports = lazyWithRetry(() => import("./Reports"), {
moduleKey: "reports",
});
The default retry count is zero because retrying a deleted hashed asset usually does not help. Retries can be enabled for applications that also experience short-lived network failures.
Some important limitations:
- A reload cannot recover a permanently unavailable asset
- Retaining old assets may be a better infrastructure solution
- The HTML entry point should be revalidated after deployment
- A full-page reload must work on nested client-side routes
- Old clients may be incompatible with a new backend
- An asset manifest must be trusted and version-compatible if used
For applications using React.lazy, Vite, Webpack, or a CDN:
- How do you handle stale chunk failures?
- Do you retain assets from previous deployments?
- Do you reload the page, show an update prompt, or use an Error Boundary?
- Have you tested this with two builds and an already-open browser tab?
I wrote more about the problem, implementation, testing strategy, and deployment considerations here:
- npm package: https://www.npmjs.com/package/react-lazy-chunk-recover
- Technical article: https://sachithsiriwardana.medium.com/handling-react-lazy-chunk-load-errors-after-deployment-1e4980a7a69a
I’d be interested to hear how other teams handle this failure mode in production.
28
u/acraswell Oct 18 '26
You don't ever cache the index.html.
10
u/mr_brobot__ Oct 18 '26
You can read the next.js router source code for some interesting stuff around this. Basically they force a reload for a few error cases.
And there’s also a hash id for the deployment that’s a part of the RSC response for a page, if it doesn’t match the current hash for the page then it will reload.
5
u/PrimaryFamous6139 NextJS Pages Router Oct 18 '26
I’ve usually found that retaining previous build assets is the more reliable fix, since a reload alone can’t recover a deleted chunk. A guarded reload is still a useful fallback, especially for long-lived tabs
1
u/banjochicken Oct 18 '26
You can serve your static assets from a CDN rather than the app server. Then this problem goes away entirely.
1
u/AbdelrhmanDev Oct 18 '26
two things together fixed this for me. first keep the old build assets around for a while after deploy, the hashed files are immutable and cheap to keep so a tab on build a can still fetch its chunks for a day or two. second catch chunkloaderror at the router level and do a hard reload once, so the worst case is the tab refreshes itself instead of a white screen. the reload on its own still fails if the user is offline mid navigation so the old assets part is what actually kills the error
1
u/MiskaMyasa Oct 18 '26
S3 + nginx
1
u/Immediate-You-9372 Oct 18 '26
A cdn wound cache it and keep it round right?
1
u/Xeon06 Oct 19 '26
Used to have a complicated setup where I kept the old chunks around at a stable URL by release. Upgraded our app and I just switched to listening for
vite:preloadErrorand refreshing the pagehttps://vite.dev/guide/build#load-error-handling
1
u/anthony-ball Oct 19 '26
I'd keep the sessionStorage guard even after fixing the infra side, because there's always a tab that sat open over a weekend. The setup I like is index.html served no-cache, old hashed chunks kept around for a week or so, and a tiny version.json the app checks on route change so the reload happens on a navigation instead of in the middle of a click. Then the lazy retry is just the safety net for whatever slips through. One thing to check is that the reload flag gets cleared once a fresh build loads cleanly, otherwise a second deploy in the same session skips recovery.
33
u/[deleted] Oct 18 '26
[removed] — view removed comment
4
u/[deleted] Oct 18 '26
[removed] — view removed comment
4
u/[deleted] Oct 18 '26
[removed] — view removed comment