link rel=modulepreload: Load JavaScript Modules Sooner

rel=modulepreload fetches, parses and compiles an ES module before a script imports it. How to use it, and why each module in the graph needs its own link.

Web performance often comes down to smart resource loading. While we’ve had preload hints for a while, modulepreload is specifically designed for the way modern JavaScript works. Let’s unpack what makes it special.

Update: One modulepreload doesn’t fetch the whole graph

I originally wrote that a single modulepreload fetches a module’s entire dependency graph. It doesn’t.

The HTML spec lets browsers fetch a preloaded module’s dependencies only as an optional optimization. MDN says the only way to make every browser preload them is to list each module (HTML spec, MDN). I’ve fixed the paragraph and the example below.

When you use <link rel="modulepreload">, you’re telling the browser: “Hey, I’m going to need this module soon - could you fetch it and get it ready?” The browser then fetches the module, parses it, and compiles it ahead of time. This is different from regular preload, which only handles the fetching part.

The real power comes when dealing with module dependencies. Without hints, the browser only finds out that main.js imports utils.js and data.js after it has fetched and parsed main.js, so the modules load one after another.

A modulepreload for each module lets the browser fetch them in parallel and put them straight into its module map. Browsers may also fetch a preloaded module’s dependencies on their own, but that’s optional, so list every module you need early.


When to use modulepreload

modulepreload excels in applications with substantial JavaScript dependencies or route-based code splitting. But consider the memory trade-off - modulepreload compiles modules upfront, consuming more memory. For smaller apps or rarely-used modules, regular dynamic imports might be more efficient.


Trevor I. Lasn

Trevor I. Lasn

Tech executive with over a decade of experience programming and building products. Building 0xinsider.com, ceodumps.com and whiteheap.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

Web performance
9 min read

How the Speculation Rules API improves web performance by prefetching and prerendering future navigations

Sep 15, 2024
Read article
Web performance
5 min read

From slow to fast: Using JavaScript's Performance API to optimize web apps

Nov 29, 2024
Read article
Web performance
5 min read

Understanding the real use cases for WebAssembly beyond the performance hype

Nov 25, 2024
Read article
Web performance
4 min read

Skeleton loading screens enhance user experience and make your app feel faster

Nov 12, 2020
Read article
Web performance
4 min read

astro-capo sorts your Astro site's head elements into 11 weighted groups so critical tags like charset come first. How to install it and use it in a layout.

Oct 19, 2024
Read article
Web performance
3 min read

How I used Cloudflare Zaraz to offload third party scripts and improve my website performance.

Oct 23, 2024
Read article
Web performance
4 min read

Using link rel='preconnect' can improve your website's performance by reducing connection setup times to key external domains.

Sep 13, 2024
Read article
Web performance
4 min read

V8's compile hints let you control which JavaScript gets compiled immediately during page load

May 12, 2025
Read article
Web performance
6 min read

Lighthouse CI fails your builds when performance drops. I run it on every pull request.

Nov 3, 2025
Read article

This article was originally published on https://www.trevorlasn.com/blog/module-preload. It was written by a human and polished using grammar tools for clarity.