Skip to content

Commit 1735afb

Browse files
committed
Changes the default for preloading pins
1 parent e0c1676 commit 1735afb

File tree

9 files changed

+25
-25
lines changed

9 files changed

+25
-25
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,19 +198,19 @@ Just like with a normal pin, you can also update a pin by running the `pin --dow
198198

199199
## Preloading pinned modules
200200

201-
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.
201+
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). All pinned modules are preloaded by default. You can opt-out of preloading by appending `preload: false` to the pin.
202202

203203
Example:
204204

205205
```ruby
206206
# config/importmap.rb
207-
pin "@github/hotkey", to: "https://ga.jspm.io/npm:@github/[email protected]/dist/index.js", preload: true
208-
pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js"
207+
pin "@github/hotkey", to: "https://ga.jspm.io/npm:@github/[email protected]/dist/index.js"
208+
pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js", preload: false
209209

210210
# app/views/layouts/application.html.erb
211211
<%= javascript_importmap_tags %>
212212
213-
# will include the following link before the importmap is setup:
213+
# will include only the following link before the importmap is setup:
214214
<link rel="modulepreload" href="https://ga.jspm.io/npm:@github/[email protected]/dist/index.js">
215215
...
216216
```

lib/importmap/map.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def draw(path = nil, &block)
2525
self
2626
end
2727

28-
def pin(name, to: nil, preload: false)
28+
def pin(name, to: nil, preload: true)
2929
clear_cache
3030
@packages[name] = MappedFile.new(name: name, path: to || "#{name}.js", preload: preload)
3131
end
3232

33-
def pin_all_from(dir, under: nil, to: nil, preload: false)
33+
def pin_all_from(dir, under: nil, to: nil, preload: true)
3434
clear_cache
3535
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
3636
end

lib/install/config/importmap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Pin npm packages by running ./bin/importmap
22

3-
pin "application", preload: true
3+
pin "application"

test/dummy/config/importmap.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
pin_all_from "app/assets/javascripts"
22

3-
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
4-
pin "not_there", to: "nowhere.js"
3+
pin "md5", to: "https://cdn.skypack.dev/md5"
4+
pin "not_there", to: "nowhere.js", preload: false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pin "md5", to: "https://cdn.skypack.dev/[email protected]", preload: true
1+
pin "md5", to: "https://cdn.skypack.dev/[email protected]"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pin 'md5', to: 'https://cdn.skypack.dev/[email protected]', preload: true
1+
pin 'md5', to: 'https://cdn.skypack.dev/[email protected]'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pin 'md5', preload: true #@2.2.0
1+
pin 'md5' #@2.2.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pin "is-svg", to: "https://cdn.skypack.dev/[email protected]", preload: true
1+
pin "is-svg", to: "https://cdn.skypack.dev/[email protected]"

test/importmap_test.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ class ImportmapTest < ActiveSupport::TestCase
44
def setup
55
@importmap = Importmap::Map.new.tap do |map|
66
map.draw do
7-
pin "application"
8-
pin "editor", to: "rich_text.js"
9-
pin "not_there", to: "nowhere.js"
10-
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
11-
12-
pin_all_from "app/javascript/controllers", under: "controllers", preload: true
13-
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", preload: true
14-
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers", preload: true
15-
pin_all_from "app/javascript/helpers", under: "helpers", preload: true
16-
pin_all_from "lib/assets/javascripts", preload: true
17-
pin_all_from "app/components", under: "controllers", to: "", preload: true
7+
pin "application", preload: false
8+
pin "editor", to: "rich_text.js", preload: false
9+
pin "not_there", to: "nowhere.js", preload: false
10+
pin "md5", to: "https://cdn.skypack.dev/md5"
11+
12+
pin_all_from "app/javascript/controllers", under: "controllers"
13+
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina"
14+
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers"
15+
pin_all_from "app/javascript/helpers", under: "helpers"
16+
pin_all_from "lib/assets/javascripts"
17+
pin_all_from "app/components", under: "controllers", to: ""
1818
end
1919
end
2020
end
@@ -97,7 +97,7 @@ def setup
9797
set_one = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "1").to_s
9898
set_two = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "2").to_s
9999

100-
@importmap.pin "something", to: "https://cdn.example.com/somewhere.js", preload: true
100+
@importmap.pin "something", to: "https://cdn.example.com/somewhere.js"
101101

102102
assert_not_equal set_one, @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "1").to_s
103103
assert_not_equal set_two, @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "2").to_s

0 commit comments

Comments
 (0)