Service worker fails to load new dynamic/code-split chunk
See original GitHub issueIs this a bug report?
Yes
Can you also reproduce the problem with npm 4.x?
Yes
Which terms did you search for in User Guide?
progressive web app service workers
Environment
node -v
: 8.4.0npm -v
: 5.6.0yarn --version
(if you use Yarn):npm ls react-scripts
(if you haven’t ejected): 1.0.17
Then, specify:
- Operating system: Windows 10
- Browser and version (if relevant): Chrome 63
Steps to Reproduce
- Set up a default CRA project
- Install
react-loadable
to code-split a certain React component on the page - Build, serve and navigate to the page
- Modify the component earlier (to change the generated hash)
- Build, serve and reload the page
Expected Behavior
The new version of the code-splitted component should be loaded correctly.
Actual Behavior
The installed service worker tries to fetch the old component chunk instead of the new one, resulting in a failure to load the new chunk. This is because the hash has changed since the last time the service worker was installed. Because of this react-loadable
interprets the error and refuses to display the new page, resulting in a blank page.
This is less than ideal, and the only thing I could come up is to force a reload on the homepage. This will trigger the new service worker to be installed which results in the correct component/chunk being loaded.
Issue Analytics
- State:
- Created 6 years ago
- Comments:26 (8 by maintainers)
Top Results From Across the Web
reactjs - Code splitting causes chunks to fail to load after new ...
When the user visits a page after a new deployment, the service worker will serve from the cache. Then if the user tries...
Read more >Code Splitting - webpack
Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which...
Read more >How to solve missing chunk code splitting errors after deploy
Code splitting is a great way to avoid huge javascript files, but weird things can happen after a deploy. Let's fix that.
Read more >Code Splitting - webpack 3 documentation
Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which...
Read more >How to Optimize JavaScript Delivery to Speed Up Your Site
Like any other file building a web page, JavaScript needs to load before it ... is using a service worker to determine how...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
Ash @jeffposnick said, talking about service workers here is just making things more confusing. Same problem, just harder to see.
Another good solution is to “cake on the built assets”. Running
yarn run build
will wipe out any old./build/
directory but on your server, you can keep the old files from the old deployment.If the Monday build was:
build/static/js/main.aaa.js
build/static/js/chunk.111.js
…and someone loads
main.aaa.js
on that Monday but don’t click the button that causeschunk.111.js
to load yet.Tuesday’s build was:
build/static/js/main.bbb.js
build/static/js/chunk.222.js
So if that Monday user eventually tries to load
chunk.111.js
it’s going to be gone. It would be better if your server (e.g. Netlify, S3, Nginx, whatever) would be available to host all of…:build/static/js/main.aaa.js
build/static/js/chunk.111.js
build/static/js/main.bbb.js
build/static/js/chunk.222.js
that you would avoid that 404 error.
Having to reload the page just because a chunk failed feels dangerous. What if the user is in the midst of typing in their shipping address and a reloaod causes all sorts of disruption to the shopping cart checkout process.
I feel your pain. I’ve been thinking about it a lot but it’s hard to come up with a unified solution as this is mostly an issue with the hosting providers. Using something like Cloudflare to cache all your bundles on their servers (up to a month) could help if you don’t have full control over the hosting of your application.