Skip to content

Commit c0a85c3

Browse files
author
David Heinemeier Hansson
committed
Flip default on preload to false
As soon as you add more pins, preloading needs to be an active process.
1 parent 74306cd commit c0a85c3

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ Just like with a normal pin, you can also update a pin by running the `pin --dow
142142

143143
## Preloading pinned modules
144144

145-
To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, we use [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload). Pinned modules are preloaded by default, but you can turn this off with `preload: false`.
145+
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.
146146

147147
Example:
148148

149149
```ruby
150150
# config/importmap.rb
151-
pin "@github/hotkey", to: "https://ga.jspm.io/npm:@github/[email protected]/dist/index.js"
152-
pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js", preload: false
151+
pin "@github/hotkey", to: "https://ga.jspm.io/npm:@github/[email protected]/dist/index.js", preload: true
152+
pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js"
153153

154154
# app/views/layouts/application.html.erb
155155
<%= javascript_importmap_tags %>

lib/importmap/map.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def draw(path = nil, &block)
2222
self
2323
end
2424

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

30-
def pin_all_from(dir, under: nil, to: nil, preload: true)
30+
def pin_all_from(dir, under: nil, to: nil, preload: false)
3131
clear_cache
3232
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
3333
end

lib/install/config/importmap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
# Use npm packages from a JavaScript CDN by running ./bin/importmap
55

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

test/dummy/config/importmap.rb

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

3-
pin "md5", to: "https://cdn.skypack.dev/md5"
4-
pin "not_there", to: "nowhere.js", preload: false
3+
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
4+
pin "not_there", to: "nowhere.js"

test/importmap_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ class ImportmapTest < ActiveSupport::TestCase
44
def setup
55
@importmap = Importmap::Map.new.tap do |map|
66
map.draw do
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"
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
1111

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"
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
1717
end
1818
end
1919
end

0 commit comments

Comments
 (0)