Skip to content

Commit 5a59187

Browse files
committed
Accept Cache Key for Inline & Preloaded Module in Importmap Tag
1 parent ad2cbb5 commit 5a59187

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Gemfile.lock

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ GEM
181181
PLATFORMS
182182
arm64-darwin-20
183183
arm64-darwin-21
184+
arm64-darwin-22
184185
x86_64-darwin-20
185186
x86_64-darwin-21
186187
x86_64-darwin-22

app/helpers/importmap/importmap_tags_helper.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Importmap::ImportmapTagsHelper
22
# Setup all script tags needed to use an importmap-powered entrypoint (which defaults to application.js)
3-
def javascript_importmap_tags(entry_point = "application", shim: true)
3+
def javascript_importmap_tags(entry_point = "application", shim: true, inline_importmap_cache_key: :json, module_preload_tags_cache_key: :preloaded_module_paths)
44
safe_join [
5-
javascript_inline_importmap_tag,
6-
javascript_importmap_module_preload_tags,
5+
javascript_inline_importmap_tag(Rails.application.importmap.to_json(resolver: self, cache_key: inline_importmap_cache_key)),
6+
javascript_importmap_module_preload_tags(cache_key: module_preload_tags_cache_key),
77
(javascript_importmap_shim_nonce_configuration_tag if shim),
88
(javascript_importmap_shim_tag if shim),
99
javascript_import_module_tag(entry_point)
@@ -34,15 +34,15 @@ def javascript_importmap_shim_tag(minimized: true)
3434
# Import a named JavaScript module(s) using a script-module tag.
3535
def javascript_import_module_tag(*module_names)
3636
imports = Array(module_names).collect { |m| %(import "#{m}") }.join("\n")
37-
tag.script imports.html_safe,
37+
tag.script imports.html_safe,
3838
type: "module", nonce: request&.content_security_policy_nonce
3939
end
4040

4141
# Link tags for preloading all modules marked as preload: true in the `importmap`
4242
# (defaults to Rails.application.importmap), such that they'll be fetched
4343
# in advance by browsers supporting this link type (https://caniuse.com/?search=modulepreload).
44-
def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap)
45-
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self))
44+
def javascript_importmap_module_preload_tags(importmap = Rails.application.importmap, cache_key: :preloaded_module_paths)
45+
javascript_module_preload_tag(*importmap.preloaded_module_paths(resolver: self, cache_key: cache_key))
4646
end
4747

4848
# Link tag(s) for preloading the JavaScript module residing in `*paths`. Will return one link tag per path element.

test/importmap_tags_helper_test.rb

+24
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,28 @@ def content_security_policy_nonce
5555
ensure
5656
@request = nil
5757
end
58+
59+
test "separate caches for inline importmap" do
60+
set_one = javascript_importmap_tags(inline_importmap_cache_key: 'inline-1')
61+
ActionController::Base.asset_host = "http://assets.example.com"
62+
63+
set_two = javascript_importmap_tags(inline_importmap_cache_key: 'inline-2')
64+
65+
assert_not_equal set_one, set_two
66+
assert_match /assets\.example\.com/, javascript_importmap_tags(set_one)
67+
ensure
68+
ActionController::Base.asset_host = nil
69+
end
70+
71+
test "separate caches for preloaded modules" do
72+
set_one = javascript_importmap_tags(module_preload_tags_cache_key: 'preload-1')
73+
ActionController::Base.asset_host = "http://assets.example.com"
74+
75+
set_two = javascript_importmap_tags(module_preload_tags_cache_key: 'preload-2')
76+
77+
assert_not_equal set_one, set_two
78+
assert_match /assets\.example\.com/, javascript_importmap_tags(set_one)
79+
ensure
80+
ActionController::Base.asset_host = nil
81+
end
5882
end

0 commit comments

Comments
 (0)