The letter S in a light blue, stylized speech bubble followed by SpeakBits
SpeakBitsThe letter S in a light blue, stylized speech bubble followed by SpeakBits
Trending
Top
New
Controversial
Search
Groups

Enjoying SpeakBits?

Support the development of it by donating to Patreon or Ko-Fi.
About
Rules
Terms
Privacy
EULA
Cookies
Blog
Have feedback? We'd love to hear it!

Optimizing SPA load times with async chunks preloading

mmazzarolo.com
submitted
8 mos ago
byboredgamertoprogramming

Summary

The next generation of web developers will be able to make the most of the power of the web. The next generation will be capable of making the most out of the internet.

While code splitting offers multiple benefits, it also has some drawbacks. By default, chunks are downloaded only when needed. This can introduce noticeable delays in two areas.

This script holds a mapping between each route and the files that should be preloaded for that route. When executed, it preloads the necessary files for the current path by manually adding them to the HTML page.

Luckily, the bundler exposes these dependencies as "chunk groups" in its API. I recommend creating a plugin that checks the compilation output to determine the names of the files each chunk depends on.

The script is executed on page load before the page load. It adds a link to each file that should be preloaded on the current path (window.location.com) The script is inserted into the HTML file before any other script.

If you plan to use this pattern in production, you’ll probably want to at least improve the following areas. For simplicity, I’ve left some implementation details to the reader.

As an alternative to the previous drawback, I’d recommend using a Service Worker to precache all your app chunks. Google’s Workbox is my go-to solution for precaching.

 screw web site website internet site site plunger plumber's helper microphone mike-0
12

4 Comments

2
throwschen
8 mos ago
I do love that a lot of these frameworks abstract this away. It's important for end users and should always be done unless explicitly opted out of.
2
boredgamerOP
8 mos ago
It's good until you're a novice who has never really understood how it works
2
throwschen
8 mos ago
Oh definitely. It's why my first recommendation is always to try and understand the lowest level you can before moving onto frameworks
2
boredgamerOP
8 mos ago
That is really great to tell juniors! I feel they always jump into the high level stuff