Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preload all by default #218

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,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:
<link rel="modulepreload" href="https://ga.jspm.io/npm:@github/hotkey@1.4.4/dist/index.js">
<link rel="modulepreload" href="/assets/javascripts/@github--hotkey.js">
...
```

Expand Down Expand Up @@ -259,7 +259,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.
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 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 "not_there", to: "nowhere.js", preload: false
6 changes: 3 additions & 3 deletions test/importmap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down