r/reactjs • u/Vaciuum1 • Oct 09 '26
Discussion users kept our react app open across deploys and lazy loaded chunks started 404ing
im a backend engineer on ella (ai video app, i work on it). people leave the tab open for hours and we deploy a lot, so the old build tried to lazy load chunks the deploy already deleted and got a 404. the fix had two parts. server side, an old chunk only gets deleted when the new vite manifest doesnt use it and its older than a week (you cant trust file age alone, ci stamps every file fresh). in the browser, when you come back to the tab it checks /version, and if its stale it reloads, but only when nothing is running. nobody wants a reload in the middle of a generation. we count open requests by wrapping fetch and EventSource:
ts
const realFetch = globalThis.fetch
globalThis.fetch = async (...args) => {
openRequests.value++
try { return await realFetch(...args) }
finally { openRequests.value-- }
}
effect(() => stale.value && openRequests.value === 0, ready => {
if (ready) location.reload()
})
plus a sessionStorage flag so it cant get stuck in a reload loop. how do you guys handle this?
74
u/slvrsmth Oct 10 '26
Don't auto reload. Users will absolutely hate you. Show a notification. Make it as prominent and annoying as you want, but the reload must come from user decision.Ā
107
u/azangru Oct 09 '26
We store static assets for a long time :-)
34
u/realbiggyspender Oct 10 '26
Assuming that the deployed chunks use content hashes for their filenames, the fix is to never delete the old ones on a new deployment. Storage is cheap.
65
u/ohx Oct 10 '26
Hash your build files. Don't cache index.html.
7
u/Lyonbane Oct 10 '26
A common solution: host your static files on a separate bucket, and on every deployment add the new ones and do not delete the old ones. You can set the old ones to be deleted after 30 days automatically. This kind of lifecycle handles most of the scenarios, and upcoming issues like multi instances having the same issue. Also make sure that static 404 requests are not cached, like they would be available in a min, but a 404 is cached and they are gone for 10mins.
6
u/YCarrott Oct 09 '26
really nice approach. one thing im curious about though, since fetch resolves as soon as the response headers arrive, couldn't openRequests hit 0 while a generation is still streaming? do you track active generations separately or is all the streaming handled through EventSource?
2
u/BlazingThunder30 Oct 10 '26
We auto reload. Our app is more traditional than frontend heavy so much of the important info is stored in the URL anyway. Bundles are hashed by filename so only the thing we change cause a 404.
The more annoying issue to solve is when we do a backend bump and an API has breaking changes. That causes very annoying errors
2
u/SnooHesitations9295 Oct 10 '26
The fetch shit should go into service worker.
2
u/Scottykl Oct 10 '26
Lol we have our old asset chunks from 2020 still kicking around. I don't think it's a big deal. Don't need to delete the old assets
2
u/imazined Oct 10 '26
If this a video editor as a web application why use chunks in the first place. They have their place but not necessarily in SPA.
Imagine users working with an application in a sporty WiFi like on a train or just an old building with thick stone walls. Suddenly they can't access features because the code wasn't loaded before.
1
u/tannerhallman Oct 10 '26
I had this problem in the past. We would detect when users navigate to new sidebar tabs, and if there was an update weād reload the page as part of the navigation.
14
u/Honey-Entire Oct 10 '26
Asking the obvious here, what does this have to do with react, Claude?
2
u/Stromedy1 Oct 11 '26
I've seen this go wrong in the other direction. A team shipped the auto-reload fix, celebrated, and then the support ticket arrived: someone had a long export running in that tab and the deploy ate it mid-job. Technically the reload worked. In the human sense it was a failure.
The chunk 404 is the symptom you're reacting to, but it's actually the least dangerous of the three races you're in. First, assets disappearing: keep old hashed chunks around for a few generations, storage is cheap and 404s just stop happening. Second, index.html cached: serve it no-cache, because if a user can hold yesterday's index pointing at today's CDN layout, nothing else you do matters.
Third is the one most teams never fix properly: that tab isn't running your app, it's running a version of your app. To a user who's had it open for six hours, a deploy isn't an event, it's the floor shifting under them. The thing that actually breaks their session isn't the 404, it's the API contract drifting while they work. You already hinted at this with the backend compatibility point and it's the right instinct.
So version each deploy as a unit and poll a small version endpoint. Let a chunk 404 be handled by retention, and let the contract change be the only thing that ever triggers the "new version available, refresh when ready" banner. The reload button is a courtesy, not a weapon.
One caution on the monkey-patch approach: counting in-flight requests is optimistic. Chunked uploads go quiet between bursts, and your "nothing is running" check can fire exactly mid-upload when the count happens to hit zero. If you ever auto-reload, gate it on an explicit busy flag your upload code sets, not on network silence.
1
u/drprofessorzed Oct 11 '26
This is over engineered imo. Just put your static assets in s3 bucket with a ttl via lifecycle rule and never delete them on deploy. No 404s and no /version endpoint needed which adds additional traffic to your server.
1
u/Embarrassed_Pin9840 Oct 11 '26
We hit this too, with one extra case: people in the middle of a long video export when a deploy lands. A forced reload would throw their work away. What worked: check /api/version when the tab becomes visible and show a small "new version, reload" banner instead of reloading; preload the chunks the export needs as soon as the editor opens, so a deploy mid-session can't break it; and if a lazy chunk still 404s, catch it and show the reload banner, not an error screen.
0
u/mattsowa Oct 12 '26
ai post