Skip to content

Commit

Permalink
Changes the default for preloading pins
Browse files Browse the repository at this point in the history
  • Loading branch information
codergeek121 committed Dec 20, 2023
1 parent e0c1676 commit 1735afb
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ 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 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.

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: "https://ga.jspm.io/npm:@github/hotkey@1.4.4/dist/index.js"
pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js", preload: false

# app/views/layouts/application.html.erb
<%= javascript_importmap_tags %>
# will include the following link before the importmap is setup:
# will include only the following link before the importmap is setup:
<link rel="modulepreload" href="https://ga.jspm.io/npm:@github/hotkey@1.4.4/dist/index.js">
...
```
Expand Down
4 changes: 2 additions & 2 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/install/config/importmap.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Pin npm packages by running ./bin/importmap

pin "application", preload: true
pin "application"
4 changes: 2 additions & 2 deletions test/dummy/config/importmap.rb
Original file line number Diff line number Diff line change
@@ -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 "md5", to: "https://cdn.skypack.dev/md5"
pin "not_there", to: "nowhere.js", preload: false
2 changes: 1 addition & 1 deletion test/fixtures/files/outdated_import_map.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pin "md5", to: "https://cdn.skypack.dev/md5@2.2.0", preload: true
pin "md5", to: "https://cdn.skypack.dev/md5@2.2.0"
2 changes: 1 addition & 1 deletion test/fixtures/files/single_quote_outdated_import_map.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pin 'md5', to: 'https://cdn.skypack.dev/md5@2.2.0', preload: true
pin 'md5', to: 'https://cdn.skypack.dev/md5@2.2.0'
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pin 'md5', preload: true #@2.2.0
pin 'md5' #@2.2.0
2 changes: 1 addition & 1 deletion test/fixtures/files/vulnerable_import_map.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pin "is-svg", to: "https://cdn.skypack.dev/is-svg@3.0.0", preload: true
pin "is-svg", to: "https://cdn.skypack.dev/is-svg@3.0.0"
24 changes: 12 additions & 12 deletions test/importmap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ 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 "md5", to: "https://cdn.skypack.dev/md5", preload: true

pin_all_from "app/javascript/controllers", under: "controllers", preload: true
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", preload: true
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers", preload: true
pin_all_from "app/javascript/helpers", under: "helpers", preload: true
pin_all_from "lib/assets/javascripts", preload: true
pin_all_from "app/components", under: "controllers", to: "", preload: true
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"

pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina"
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers"
pin_all_from "app/javascript/helpers", under: "helpers"
pin_all_from "lib/assets/javascripts"
pin_all_from "app/components", under: "controllers", to: ""
end
end
end
Expand Down Expand Up @@ -97,7 +97,7 @@ def setup
set_one = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "1").to_s
set_two = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "2").to_s

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

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

0 comments on commit 1735afb

Please sign in to comment.