Skip to content

Commit eb34334

Browse files
authored
Preload all by default (#218)
* Preload all by default * Fix test fixtures
1 parent 23575bb commit eb34334

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ Unpinning and removing "object-assign"
115115

116116
## Preloading pinned modules
117117

118-
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.
118+
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.
119119

120120
Example:
121121

122122
```ruby
123123
# config/importmap.rb
124-
pin "@github/hotkey", to: "https://ga.jspm.io/npm:@github/hotkey@1.4.4/dist/index.js", preload: true
125-
pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js"
124+
pin "@github/hotkey", to: "vendor/javascript/@github--hotkey.js"
125+
pin "md5", to: "vendor/javascript/md5.js", preload: false
126126

127127
# app/views/layouts/application.html.erb
128128
<%= javascript_importmap_tags %>
129129
130130
# will include the following link before the importmap is setup:
131-
<link rel="modulepreload" href="https://ga.jspm.io/npm:@github/hotkey@1.4.4/dist/index.js">
131+
<link rel="modulepreload" href="/assets/javascripts/@github--hotkey.js">
132132
...
133133
```
134134

@@ -177,7 +177,7 @@ Pin your js file:
177177
```rb
178178
# config/importmap.rb
179179
# ... other pins...
180-
pin "checkout"
180+
pin "checkout", preload: false
181181
```
182182

183183
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.

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

test/dummy/config/importmap.rb

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

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

test/importmap_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ 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"
7+
pin "application", preload: false
8+
pin "editor", to: "rich_text.js", preload: false
9+
pin "not_there", to: "nowhere.js", preload: false
1010
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
1111

1212
pin_all_from "app/javascript/controllers", under: "controllers", preload: true

0 commit comments

Comments
 (0)