Use rel="modulepreload" when an important JavaScript module starts downloading late because the browser first has to discover it in another file. A good candidate has a visible consequence: bringing the request forward lets a visitor do something sooner. Pick that action before changing the page.
Follow the visitor's first action back to its import
Consider a booking page where the dates are already visible but the calendar opens only after the application attaches its event handler. The HTML loads the entry module. That module imports a booking controller, which in turn imports the calendar. On a fresh visit the browser can spend part of the wait learning which file it needs next. The calendar itself might be small. Its position at the end of the chain is what makes it interesting.
Start with a production build and a direct visit to the affected URL. A development server can use different filenames, produce more modules and serve code that never reaches customers. Record the Network and Performance panels during a reload and a first attempt to open the calendar. Preserve the trace before altering anything. Note the viewport, network and CPU settings, browser version, authentication state and whether a service worker controls the page. A later comparison needs the same conditions.
Find the downloaded file that contains the calendar and inspect its initiator. Follow the initiators back toward the entry script. Check where the download starts in relation to its parent and when the calendar actually responds. A late request is a lead. It becomes a candidate when you can associate the dependency with the delayed action. If the calendar responds before that file arrives, you have probably found another feature packaged under a misleading name.
Separate discovery time from the work after download
Read the request's timing breakdown instead of judging the length of the entire bar. A script discovered early can spend time queued behind other requests, waiting for the server or downloading bytes. Moving the discovery point may change little in those cases. Another file may finish early but wait on a busy main thread before its code can run. Compare the end of the transfer with the execution trace and the first visible response to the visitor.
For the booking example, run a second trial in which you click after the module graph has finished loading. If the same slow animation or frozen input remains, investigate the work done when opening the calendar. The guide to tracing JavaScript execution and long tasks explains how to inspect that part of the delay. An early download does not reduce the amount of computation in an expensive handler.
It also matters whether everyone on that route needs the feature. A calendar displayed as part of the initial booking flow presents a different trade-off from a tax calculator tucked inside an optional settings panel. Keep the latter lazy by default. If evidence points to a narrower group that needs the module immediately, such as people landing directly on a date-selection step, scope the experiment to that document.
Put the hint in the HTML that needs it
Start with one known dependency from the production build. Suppose the entry script imports a controller that resolves its calendar import to /assets/calendar.81c9.js. The route's server-rendered document can give the browser that address beside the ordinary entry script. The filenames below are illustrative.
<link rel="modulepreload" href="/assets/calendar.81c9.js">
<script type="module" src="/assets/booking.462b.js"></script>The browser now has enough information to fetch the calendar when it reads the document. The original imports still determine when the code is evaluated. Keep the entry script and the real import in place. A preload link alone will not attach the button handler, open a calendar or establish the application’s normal initialization order.
The HTML Standard's definition of modulepreload explains the special treatment of module scripts. The fetched module goes into the document's module map for later use. A generic preload with a script destination follows a different processing model. A browser may also fetch dependencies of a module it preloads. Do not depend on every browser recursively fetching the whole graph. If a particular child must be discovered early, test a hint naming that child explicitly.
Generate the URL from the same build manifest or asset lookup used by the application. A handwritten hash can survive a deployment after the module filename has changed. The hint would then request an obsolete file while the entry script imports the new one. Check both the delivered HTML and the URL the browser ultimately requests. A redirect, a different query string or a changed asset base can explain an apparently redundant transfer.
Check credentials, route ownership and existing hints
A straightforward first trial uses a public same-origin module. If the assets live on a separate origin, inspect the actual module request and the response's CORS headers. Module fetches use CORS. The crossorigin setting affects credentials, and an explicit use-credentials deserves the same scrutiny as on the original module script. A URL that works when pasted into a tab can still fail when fetched as a module by another origin. Look at the console and the failed request in context.
Inspect the HTML the browser receives, not only the template you intend to change. A framework or build plugin may already emit preload links from its dependency graph. If it already names the correct child in this route's head, a second tag gives you little to test. Look instead at request queuing, the asset response or the time spent executing the module. If a client-side component inserts the hint after the parent imports the child, it also arrives too late to test earlier discovery.
Check the neighboring routes at the same time. A shared layout may cover search, account settings and checkout as well as the booking form. Adding the link to that layout changes the cost of every direct landing. Attach it to the smallest template that covers the first-use case. Verify the final HTML for a page outside that template and check that it does not start a calendar transfer.
Compare the two builds with the same journey
Use the original build and the one-hint build under matched conditions. Run each several times with a cold cache to expose discovery and download costs. Then visit again with normal caching enabled. A returning visitor may already have the file locally. Mixing these results can make an improvement disappear or make a single slow server response look like a win. Keep the traces and the exact build identifiers together.
| Question | What to inspect |
|---|---|
| Did discovery change? | The calendar request now starts from the document and overlaps its parent's transfer. |
| Was the fetched file used? | The import resolves to the hinted module and the calendar opens correctly. |
| Did other work move later? | Compare the first visible content, critical stylesheet and main image across runs. |
| Did the visitor gain anything? | Repeat the same first attempt, recording the first usable response and any ignored input. |
Make the interaction test precise enough for someone else to repeat it. For example, attempt to open the calendar as soon as the visible control is enabled, choose a date and check that the chosen value reaches the form. If the early click is lost before the listener attaches, record the loss. Counting only successful clicks would hide the problem the experiment was supposed to fix. Keep an eye on keyboard activation as well as touch or pointer input.
Decide whether the hint earns its transfer
If the request advances and the calendar becomes usable earlier without a repeatable regression in other critical work, keep the route-specific change and document the dependency it covers. If only the request moves, return to the trace and find the next actual wait. A shortened row in a waterfall is insufficient grounds to ship extra network work to every visitor.
Keep one deployment check that compares the hinted URL against the current production manifest. Exercise a direct visit after a build change, especially when code splitting or route ownership changes. When the calendar moves into the entry bundle, becomes optional or stops being the first interaction, remove the obsolete hint and rerun the same journey. The original trace and test steps make that decision considerably easier than trying to remember why a hashed filename appeared in the head.