Posted about 3 years ago. Visible to the public.

Unpoly: Loading large libraries on-demand

When your JavaScript bundle is so massive that you cannot load it all up front, I would recommend to load large libraries from the compilers that need it.

Compilers are also a good place to track whether the library has been loaded before. Note that including same <script> tag more than once will cause the browser to fetch and execute the script more than once. This can lead to memory leaks or cause duplicate event handlers being registered.

In our work we mostly load all JavaScript up front, since our bundles are small enough. We recently had a case where we had a very large geo map library that we only wanted to load when it is used. The code we used for that looked something like this:

Copy
// get_script.js let scriptsLoaded = {}; function loadScript(url) { function createScriptTag(url) { let scriptTag = document.createElement("script") scriptTag.src = url return scriptTag } let cachedPromise = scriptsLoaded[url] if (cachedPromise) { return cachedPromise } else { let promise = new Promise((resolve) => { let scriptTag = createScriptTag(url) scriptTag.addEventListener('load', resolve) document.body.appendChild(scriptTag) }) scriptsLoaded[url] = promise return promise } } // map.js up.compiler('.map', function(map) { await loadScript('/huge-lib.js') HugeLibrary.init(map) })

Note how the library will only get loaded once even after compiling multiple <div class="map"> elements.

Webpacker

See Webpacker: Loading code on demand for extracting code into a separate file.

In the code above you may delete the loadScript() function and instead use Webpack's import() function:

Copy
up.compiler('.map', async function(map) { await import('/huge-lib.js') HugeLibrary.init(map) })

Rails asset pipeline

As in newer Rails all assets are precompiled and the path above /huge-lib.js will at least not exist in production. Here is our preferred way to solve this:

1. Copy the large library to your vendor folder e.g. vendor/asset-libs/huge-lib/huge-lib.js

2. Create a new file e.g. app/assets/javascripts/huge-lib.js with the following content:

Copy
//= require huge-lib.js

3. Allow this file to be precompiled by adding the following line to your config/application.rb

Copy
config.assets.precompile += ['huge-lib.js']

4. Finally change the compiler from e.g. map.js to map.js.erb and use the asset_path helper:

Copy
loadScript('<%= asset_path('huge-lib', type: :javascript) %>')

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
about 1 month ago
by Arne Hartherz
Keywords:
lazy, load, javascript, huge, on, demand, script, loader, scriptloader, getscript
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more