Here's an unexpected issue I've run into with Webpack code splitting in the wild: Imagine this scenario:
- The user loads a React app with Webpack code splitting and a few bundle chunks are loaded
- A deploy happens and the contents of any future chunks that the user might receive from the server are updated (note: the previous chunks get deleted on the server during a deploy)
- The user clicks on a link and loads a new route which triggers more bundle chunks to load. Except these new chunks are incompatible with the ones the user's browser has already loaded and the app breaks because of a runtime error
How can this scenario be prevented?
One possible solution would be to maintain multiple versioned sets of chunks but I'm wondering if there's a simpler solution being used by large-scale apps.
If preload-webpack-plugin is used, all chunks can be prefetched but they will only stay cached for a short time (5 minutes in Chrome).
Here's an unexpected issue I've run into with Webpack code splitting in the wild: Imagine this scenario:
- The user loads a React app with Webpack code splitting and a few bundle chunks are loaded
- A deploy happens and the contents of any future chunks that the user might receive from the server are updated (note: the previous chunks get deleted on the server during a deploy)
- The user clicks on a link and loads a new route which triggers more bundle chunks to load. Except these new chunks are incompatible with the ones the user's browser has already loaded and the app breaks because of a runtime error
How can this scenario be prevented?
One possible solution would be to maintain multiple versioned sets of chunks but I'm wondering if there's a simpler solution being used by large-scale apps.
If preload-webpack-plugin is used, all chunks can be prefetched but they will only stay cached for a short time (5 minutes in Chrome).
Share Improve this question edited Aug 3, 2017 at 21:16 Maros asked Aug 3, 2017 at 19:51 MarosMaros 1,9734 gold badges27 silver badges58 bronze badges 7 | Show 2 more comments4 Answers
Reset to default 2As Max Stoiber writes on spectrum.chat:
ServiceWorkers come in really handy when doing code splitting!
We use the excellent offline-plugin by @nekr to cache all the current bundles locally, so no matter if the server updates the files or not the ServiceWorker will always serve the files from the local cache. Every hour it will check the server for updates and, if an update is available, download all the fresh bundles from the remote server and cache them locally. The next time the user restarts the app the new version of the app is used!
window.navigation.reload()
or use long polling and check the latest version from somewhere, for example, from latestindex.html
– BILL Commented Sep 3, 2020 at 14:41