Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readPerformance

Break request chains with modulepreload

Start fetching a JavaScript module chain in parallel with modulepreload links and check the result in the Network panel.

The network panel shows your JavaScript as a staircase. main.js loads, then app.js starts, then chart.js starts, each one waiting for the file before it. Even tiny files can add up when the requests run in sequence.

Why the browser finds imports late

Save each section in its own file using the name in the comment. Serve the files from your local server’s web root. With only the entry point in the HTML, the browser discovers each dependency when it parses that dependency's importer.

// main.js
import { render } from './app.js'

render(document.querySelector('#out'))

// app.js
import { drawChart } from './chart.js'

export function render(el) {
  el.textContent = drawChart([3, 1, 2])
}

// chart.js
export function drawChart(values) {
  return values.map((value) => '#'.repeat(value)).join(' ')
}

In your HTML, put <script type="module" src="/main.js"></script> in the head and <p id="out"></p> in the body. You should see ### # ##. With a 300 ms response delay per JavaScript file, Chromium took about 900 ms to finish the chain.

diagram

The import chain with and without modulepreload

The modulepreload links start both dependency requests alongside the entry script.

List the chain in the head

You know the chain, so tell the browser before it has to discover it. Add one modulepreload link per dependency, above the entry script.

<head>
  <link rel="modulepreload" href="/app.js" />
  <link rel="modulepreload" href="/chart.js" />
  <script type="module" src="/main.js"></script>
</head>

The page still renders ### # ##. With the same server delay, the requests started within a few milliseconds of each other and finished in about 300 ms.

The browser fetches and parses the module and stores it in the document's module map. When main.js asks for ./app.js, the browser can reuse the module fetch, even while it is still in progress. See the modulepreload processing rules.

A browser can also fetch a preloaded module's dependencies. List each dependency you want the browser to discover from the HTML. Chrome added support in 66, Firefox in 115 and Safari in 17.

Match the module's fetch settings

Replace both links with rel="preload" as="script" and leave off crossorigin to reproduce a common mistake. Chromium issues two requests for each dependency in this fixture and logs a credentials-mode mismatch. For a plain preload to be eligible for reuse, its request mode and credentials mode must match the consuming request. See the preload matching rules.

modulepreload fetches in CORS mode by default. Match the link's crossorigin setting to the module graph's entry script, including for same-origin URLs when you use use-credentials.

Before you add tags by hand

Bundlers can combine static imports into one chunk or emit shared chunks. Vite also adds preload steps for dependencies of code-split dynamic imports. Check the built HTML and Network panel before adding links for modules the page needs at startup.