Overview
Tools like Create React App, Vite, etc. use code splitting to improve production app performance. It’s certainly a best practice. For example, when your app is built for prod you’ll see something like this in the final build:
main.js
chunk-abc123.js
chunk-def456.js
Problem
When netlify deploys a new version of your app, files from any previous deployment are immediately deleted from the interwebs. Imagine a user had your app open and their browser hasn’t yet requested chunk-def456. Then you make a new deployment. At that moment, netlify immediately deletes chunk-def456, because it was part of the previous build.
If your user clicks on a route that needs chunk-def456, your app will break for that user. Read more: [Support Guide] Why do I see “Uncaught SyntaxError: Unexpected token <” errors? How can I use chunking or versioning for my assets at Netlify?
Also note that adding hashes to filenames (considered a best practice by many) will cause the same sort of issue.
Questions for Netlify Team
- You mention code splitting in that link above, but don’t mention any potential solutions. What are your suggestions? Options I can think of:
- Turn off code splitting (really not good, lose huge performance benefits)
- Some mechanism to force-refresh a user’s browser when new versions are deployed (also bad for many reasons - it’s unreliable, annoying to end users, and much more…imagine being halfway through filling out a form or halfway through meeting, and the app just decides to refresh)
-
You should really add this behavior in bright bold alerts in your SPA docs. A lot of SPA frameworks and tools implement code splitting and/or file hashing by default ---- for everyone that uses those tools, using Netlify results in broken apps.
-
I know you’re probably not going to change how your deploys behave, but as others have noted in previous threads, you would make Netlify apps a lot more robust and easier to set up if you did not immediately delete assets from previous builds. To be upfront, not having this behavior and it’s problems for end users clearly outlined in your SPA related docs makes us hesitant to continue using Netlify long-term.