Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Splitting the bundle on static websites

Splitting the bundle on static websites

Matthew Claffey

November 30, 2018
Tweet

More Decks by Matthew Claffey

Other Decks in Technology

Transcript

  1. Pros Slides.ihd93usdewfj.version • Easy to just output one bundle •

    Good for HTTP/1 • Large files compress better with GZIP
  2. Cons Slides.ihd93usdewfj.version • Really easy to invalidate caches • Bloated

    pages • More JS the more to parse • Costly to the users data • One error can potential break the whole bundle • Polyfill’s can find their way in easily • Vendor files get downloaded on pages that may not need them
  3. What we already have in place… Slides.ihd93usdewfj.version • Http/2 •

    Service Worker caching static assets & offline page • ?cb=querystring cachebusters • One fat bundle L • Stupid amounts of third party JavaScript Our challenge: Load only the JavaScript that the page needs rather than sending it all.
  4. The biggest unknowns… Slides.ihd93usdewfj.version • Race conditions when splitting the

    bundles (mainly around polyfills & vendor) • Will it impact the current caching strategy in place?
  5. How it works Slides.ihd93usdewfj.version Website User agent header contains –

    MSIE ?features=picturefill,Promise Cdn.polyfill.io Request Send back picturefill and Promise polyfills
  6. What’s in our vendor? Slides.ihd93usdewfj.version • JQuery 95kb • Slick-Carousel

    45kb • Masonry-Layout 23kb • Lazysizes 7kb • Imagesloaded 5.5kb
  7. What’s in our vendor? Slides.ihd93usdewfj.version • JQuery 95kb – legacy

    components use it and a carousel plugin • Slick-Carousel 45kb – components use this • Masonry-Layout 23kb – one component uses this • Lazysizes 7kb – needed on all pages • Imagesloaded 5.5kb – one component uses this
  8. Pros Slides.ihd93usdewfj.version • Easy to do • Works well with

    our current caching strategy • Cache would rarely change • Gzip would love it • Defer removes race conditions
  9. How it looks on the network Slides.ihd93usdewfj.version This doesn’t need

    jQuery - This needs jQuery - jQuery loads - component-one~component-two~component-three.js
  10. What's wrong with 0.chunk.js Slides.ihd93usdewfj.version • Website error logs would

    track a number instead of a component name if they ever 404 • Debugging would be a nightmare
  11. Iteration 2 Slides.ihd93usdewfj.version • Remove jQuery & replace slick carousel

    with a vanilla alternative – potential loss of 34kb off each page. • Server side rendering of the split components so we can preload them & remove the dependency for one file to load all scripts.
  12. Summary Slides.ihd93usdewfj.version • Don’t over complicate how you load your

    polyfills in. Use polyfill.io to do the hard work for you. • Put all files in one vendor bundle or let webpack figure out when to download a certain vendor with dynamic imports • Use dynamic imports to: • Check if components exist in the page before loading • Lazyload components on scroll (optional) • Lazyloading when interacted with (optional)