Skip to content

Changes the default for preloading pins #211

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -215,6 +215,8 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/[email protected]/md5.js"
...
```

You can preload all your pins by default by setting `config.importmap.preload_by_default` in any environment file to `true`.

## Composing import maps

By default, Rails loads import map definition from the application's `config/importmap.rb` to the `Importmap::Map` object available at `Rails.application.importmap`.
1 change: 1 addition & 0 deletions lib/importmap/engine.rb
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ class Engine < ::Rails::Engine
config.importmap.sweep_cache = Rails.env.development? || Rails.env.test?
config.importmap.cache_sweepers = []
config.importmap.rescuable_asset_errors = []
config.importmap.preload_by_default = false

config.autoload_once_paths = %W( #{root}/app/helpers )

4 changes: 2 additions & 2 deletions lib/importmap/map.rb
Original file line number Diff line number Diff line change
@@ -25,12 +25,12 @@ def draw(path = nil, &block)
self
end

def pin(name, to: nil, preload: false)
def pin(name, to: nil, preload: Rails.application.config.importmap.preload_by_default)
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: Rails.application.config.importmap.preload_by_default)
clear_cache
@directories[dir] = MappedDir.new(dir: dir, under: under, path: to, preload: preload)
end
37 changes: 37 additions & 0 deletions test/importmap_tags_helper_test.rb
Original file line number Diff line number Diff line change
@@ -69,4 +69,41 @@ def content_security_policy_nonce
refute_includes importmap_html, %{<link rel="modulepreload" href="/bar.js">}
assert_includes importmap_html, %{<script type="module">import "foo"</script>}
end

test "with preload by default disabled" do
with_preload_by_default(false) do
importmap = Importmap::Map.new
importmap.pin "foo"
importmap.pin_all_from "app/javascript/controllers"
importmap_html = javascript_importmap_tags("foo", importmap: importmap)

refute_includes importmap_html, %{<link rel="modulepreload" href="/foo.js">}
refute_includes importmap_html, %{<link rel="modulepreload" href="/goodbye_controller.js">}
end
end

test "with preload by default enabled" do
with_preload_by_default(true) do
importmap = Importmap::Map.new
importmap.pin "foo"
importmap.pin_all_from "app/javascript/controllers"
importmap_html = javascript_importmap_tags("foo", importmap: importmap)

assert_includes importmap_html, %{<link rel="modulepreload" href="/foo.js">}
assert_includes importmap_html, %{<link rel="modulepreload" href="/goodbye_controller.js">}
end
end

private

def with_preload_by_default(bool)
old_preload_setting = Rails.application.config.importmap.preload_by_default

begin
Rails.application.config.importmap.preload_by_default = bool
yield
ensure
Rails.application.config.importmap.preload_by_default = old_preload_setting
end
end
end