8

I'm interested in splitting my initial code in two chunks which are loaded asynchronously by the application using async/defer attributes. One for vendored libraries which rarely change and are big so that they could benefit from caching for a longer time, and another one containing the basic code required to load the initial page. This would be a smaller chunk which would probably change every week since we often deploy changes to production.

But we are interested in displaying a proper "loading..." page until all JS resources are downloaded. In case the app JS finishes loading before the vendor one (when vendor is not cached, this is very likely to happen) it should wait for the vendor one to be loaded.

I wasn't able to figure out how to achieve this using webpack. There are examples loading the vendor chunk synchronously so that the app code will always load after vendor, but it means the app won't render the proper "loading..." page due to it being blocked by the JS script. I could add the script to the end of the body but it's not optimal because it will increase the latency for the scripts to start downloading in comparison to adding the async script tags in the head of the document.

The code splitting documentation has an example on how to generate the vendor and app JS, but it only works if vendor is loaded before the app JS. Is there any way I can instruct the app bundle to wait for vendor to be loaded before running?

rosenfeld
  • 1,730
  • 15
  • 19
  • I ended up with some hack to achieve this for the time being: http://rosenfeld.herokuapp.com/en/articles/2016-02-29-getting-an-spa-to-load-the-fastest-possible-way-and-how-webpack-can-help-you#n149 – rosenfeld Mar 01 '16 at 23:50
  • Nice one, do you use this now or found a better solution? Based on your hack, i am checking for `VENDORS_LOADED` and then `document.createElement('script')`. What might go wrong? – Yevhenii Ponomar Jan 24 '17 at 21:30
  • I'm still using that hack but I didn't have time to upgrade to latest webpack yet, so I'm not sure it will work out of the box or if I'll have to update it to work with the latest version. Regarding to your question I'm not really sure what you're talking about. Would you mind commenting in that article instead and we can continue that discussion from there? – rosenfeld Jan 25 '17 at 13:42

0 Answers0