skip to content

In importmap-rails, what does the preload: option on pin and pin_all_from control, what is its default, and when would you change it?

level: middleimportance: should knowfreq 36%

answer

  1. modulepreload link in the head
  2. default preload: true
  3. false means fetched only on import
  4. entry-point name or array
  5. javascript_importmap_tags("booking")

basics

~20 s

preload: decides whether javascript_importmap_tags emits a modulepreload link for a pin. It defaults to true, preloading every pin on every page; false defers a module until imported; an entry-point name preloads it only for that entry point.

solid answer

~40 s

Every pin is in the import map either way; `preload:` only decides whether `javascript_importmap_tags` also emits `<link rel="modulepreload">` for it, which avoids the waterfall of discovering nested imports one level at a time. In importmap-rails 2.2 both `pin` and `pin_all_from` default to `preload: true`, so every pin is fetched on every page. Set `preload: false` for heavy code used on few pages: the browser fetches it when something imports it. Or pass an entry-point name or array, such as `preload: "booking"`, and the link appears only when the layout calls `javascript_importmap_tags("booking")`; `true` pins are preloaded for every entry point. In a campsite app the date picker belongs on the booking page only, so `preload: false` plus an import from the controller that needs it keeps the home page light.

code

ruby · 5 lines
ruby
# config/importmap.rb
pin "application"
pin "flatpickr", preload: false                 # fetched only when imported
pin "leaflet", preload: "booking"               # preloaded on the booking entry point
pin_all_from "app/javascript/campsites", under: "campsites"

go deeper

for a junior

Recall that preload: controls modulepreload links and defaults to true in importmap-rails 2.2.

for a middle

Explain the three value kinds, how entry points select pins, and why preload: false still leaves the module importable.

for a senior

Use the network panel to decide what to preload, and wire page-specific code with javascript_import_module_tag or a separate entry point.

for a principal

Balance waterfall latency against wasted bytes across page types, and decide when module count signals that a bundler would serve users better.

## What preloading means here With import maps the browser discovers modules as it parses them: it loads `application.js`, sees an import, fetches that file, sees its imports, and so on. That chain is a **waterfall**. A `<link rel="modulepreload" href="...">` tells the browser to fetch a module before anything imports it, so the chain flattens. **importmap-rails** decides which pins get such a link with the `preload:` option. Preloading is separate from mapping. A pin with `preload: false` is still in the `<script type="importmap">` JSON and can still be imported; it just is not fetched until something asks for it. ## The signatures and the default ```ruby pin(name, to: nil, preload: true, integrity: true) pin_all_from(dir, under: nil, to: nil, preload: true, integrity: true) ``` In importmap-rails 2.2, the version a Rails 8.1 app installs, **`preload:` defaults to `true`** for both. Every pin of a fresh app — `application`, Turbo, Stimulus, every Stimulus controller — gets a modulepreload link on every page. ## The three kinds of value | Value | Link emitted when | Typical use | |---|---|---| | `true` (default) | always, for every entry point | code nearly every page runs | | `false` | never; fetched when imported | heavy code used on few pages | | `"booking"` or `["application", "booking"]` | only when `javascript_importmap_tags` is called with a matching entry point | code tied to one area or layout | The selection lives in `Importmap::Map#preloaded_module_packages`: a pin whose `preload` is `true` or `false` is taken at face value; otherwise it is included only if its names intersect the entry point passed to the helper. ## The campsite date picker The booking form is the only page that needs a date picker. Three ways to wire it: 1. **Lazy import.** `pin "flatpickr", preload: false`. The `stay_dates` Stimulus controller imports it; the browser fetches it when that controller's module loads. Note that the controller itself is still preloaded through `pin_all_from`, and its static import pulls the picker in on every page that loads the controller module, so lazy-loaded controllers or a dynamic `import()` are what really defer it. 2. **Page-specific module.** Pin a `booking` module with `preload: false` and render `javascript_import_module_tag "booking"` from the booking view, after the layout's `javascript_importmap_tags` (the README stresses that order). 3. **Separate entry point.** Give booking pages their own layout that calls `javascript_importmap_tags("booking")`, and pin the picker with `preload: "booking"`. That layout imports a `booking` module instead of `application`, and preloads `true` pins plus those tagged `booking`. ## pin_all_from and module names `pin_all_from "app/javascript/campsites", under: "campsites"` pins every `.js` (and `.mjs`) file below the folder: - `app/javascript/campsites/map.js` becomes module `campsites/map`; - `app/javascript/campsites/index.js` becomes module `campsites`, because a trailing `index` is dropped; - `to:` changes the asset path prefix if the files sit somewhere other than the logical name suggests. The `preload:` given to `pin_all_from` applies to every file it expands to. ## Why the default is true, and when to resist it - Most apps built on import maps have a modest amount of JavaScript, and preloading everything removes the waterfall entirely. - Each preload is still a download. A large charting or editor library preloaded on every page costs bandwidth for users who never open that page. - Measure with the browser's network panel: a module requested late, after a chain of others, is a candidate for `true`; a large module downloaded on pages that never use it is a candidate for `false` or an entry-point name. ## Changing it from the command line `bin/importmap pin flatpickr --preload false` writes `preload: false` on the new pin, and `bin/importmap update` preserves an existing pin's `preload:` value when it re-pins a newer version. ## Checking the result - View the page source: every `<link rel="modulepreload">` corresponds to a pin selected for that entry point, so a missing or unexpected one points straight at a `preload:` value. - Open the network panel with the cache disabled. Preloaded modules start together near the top of the waterfall; a non-preloaded module starts only after the file that imports it has arrived. - Compare two page types, such as the campsite search page and the booking page. If the search page downloads the date picker it never shows, the pin's `preload:` or the way the controller imports it needs another look.

  • Where must javascript_import_module_tag go relative to javascript_importmap_tags?
    After it. `javascript_import_module_tag "booking"` renders a module script that imports the name, and the browser can resolve that name only once the import map has been declared. A `content_for :head` block yielded below `javascript_importmap_tags` in the layout is the usual arrangement.
  • Does preload: false remove a pin from the import map JSON?
    No. The JSON built by `Importmap::Map#to_json` includes every pin regardless of `preload:`; the option only affects which `<link rel="modulepreload">` tags `javascript_importmap_tags` renders. A non-preloaded module is still importable by name, statically or with `import()`.

  • preload: false removes the module from the import map entirely
  • Pins are not preloaded unless you add preload: true to each one
  • preload: accepts only true or false
  • A module with preload: true is executed on every page even if nothing imports it
  • pin_all_from ignores preload: and always preloads every file

Assumes importmap-rails 2.2 as installed by a Rails 8.1 app, where pin and pin_all_from default to preload: true and accept an entry-point name or array.