From 55d9683096f1d8cb4626de290887786d071e5d59 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 1 Jan 2024 14:00:34 +0100 Subject: [PATCH 1/2] Preload all by default --- README.md | 10 +++++----- lib/importmap/map.rb | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 01de2ee..9df2c9d 100644 --- a/README.md +++ b/README.md @@ -198,20 +198,20 @@ Just like with a normal pin, you can also update a pin by running the `pin --dow ## Preloading pinned modules -To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, importmap-rails supports [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload). Pinned modules can be preloaded by appending `preload: true` to the pin. +To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, importmap-rails uses [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload) by default. If you don't want to preload a dependencies, because it'you want to load it on-demand for efficiency, pinned modules can prevent preloading by appending `preload: false` to the pin. Example: ```ruby # config/importmap.rb -pin "@github/hotkey", to: "https://ga.jspm.io/npm:@github/hotkey@1.4.4/dist/index.js", preload: true -pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js" +pin "@github/hotkey", to: "vendor/javascript/@github--hotkey.js" +pin "md5", to: "vendor/javascript/md5.js", preload: false # app/views/layouts/application.html.erb <%= javascript_importmap_tags %> # will include the following link before the importmap is setup: - + ... ``` @@ -260,7 +260,7 @@ Pin your js file: ```rb # config/importmap.rb # ... other pins... -pin "checkout" +pin "checkout", preload: false ``` Import your module on the specific page. Note: you'll likely want to use a `content_for` block on the specifc page/partial, then yield it in your layout. diff --git a/lib/importmap/map.rb b/lib/importmap/map.rb index 3ef8107..bd1332a 100644 --- a/lib/importmap/map.rb +++ b/lib/importmap/map.rb @@ -25,12 +25,12 @@ def draw(path = nil, &block) self end - def pin(name, to: nil, preload: false) + def pin(name, to: nil, preload: true) clear_cache @packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload) end - def pin_all_from(dir, under: nil, to: nil, preload: false) + def pin_all_from(dir, under: nil, to: nil, preload: true) clear_cache @directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload) end From 31ced447db943c63ed889112b60fb0ca5562340b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 1 Jan 2024 14:10:16 +0100 Subject: [PATCH 2/2] Fix test fixtures --- test/dummy/config/importmap.rb | 2 +- test/importmap_test.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/dummy/config/importmap.rb b/test/dummy/config/importmap.rb index 481d62b..850ca72 100644 --- a/test/dummy/config/importmap.rb +++ b/test/dummy/config/importmap.rb @@ -1,4 +1,4 @@ pin_all_from "app/assets/javascripts" pin "md5", to: "https://cdn.skypack.dev/md5", preload: true -pin "not_there", to: "nowhere.js" +pin "not_there", to: "nowhere.js", preload: false diff --git a/test/importmap_test.rb b/test/importmap_test.rb index fb51dab..bf1bd1e 100644 --- a/test/importmap_test.rb +++ b/test/importmap_test.rb @@ -4,9 +4,9 @@ class ImportmapTest < ActiveSupport::TestCase def setup @importmap = Importmap::Map.new.tap do |map| map.draw do - pin "application" - pin "editor", to: "rich_text.js" - pin "not_there", to: "nowhere.js" + pin "application", preload: false + pin "editor", to: "rich_text.js", preload: false + pin "not_there", to: "nowhere.js", preload: false pin "md5", to: "https://cdn.skypack.dev/md5", preload: true pin_all_from "app/javascript/controllers", under: "controllers", preload: true