We are developing a Vue.js application based on Vue CLI 3 with Vue Router and Webpack. The routes are lazy-loaded and the chunk file names contain a hash for cache busting. In general, everything is working fine.
However, there is a problem during the deployment. Steps to reproduce are the following.
- User opens the application (let's assume route "/"), thus the main chunk file is loaded.
- We change something in the application and deploy a new version.
- Old chunk files are removed
- New chunk files are being added (i.e. hashes in the chunk file names change)
- User clicks a link to another route (e.g. "/foo")
- An error occurs as the application tries to load a chunk file that has been renamed:
Error: "Loading CSS chunk foo failed. (/assets/css/foo.abc123.css)"
(this might be CSS or JavaScript)
- An error occurs as the application tries to load a chunk file that has been renamed:
What is the best way to avoid errors like this?
One approach that should work is just to retain old chunk files and delete them at a later time. That, however, complicates the deployment of new versions as you need to keep track of old versions and always also deploy the old chunk files with the new version.
Another (naive) approach is to just reload as soon as such an error is detected (e.g. Vue Lazy Routes & loading chunk failed). It somewhat works, but it reloads the old route, not the new one. But at least it ensure that consecutive route changes work again.
Any other ideas? Maybe there is something in webpack that could fix this?