Skip to content

Accept Cache Key for Inline & Preloaded Module in Importmap Tag #176

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 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ GEM
PLATFORMS
arm64-darwin-20
arm64-darwin-21
arm64-darwin-22
x86_64-darwin-20
x86_64-darwin-21
x86_64-darwin-22
Expand Down
12 changes: 6 additions & 6 deletions app/helpers/importmap/importmap_tags_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Importmap::ImportmapTagsHelper
# Setup all script tags needed to use an importmap-powered entrypoint (which defaults to application.js)
def javascript_importmap_tags(entry_point = "application", shim: true)
def javascript_importmap_tags(entry_point = "application", shim: true, inline_importmap_cache_key: :json, module_preload_tags_cache_key: :preloaded_module_paths)
safe_join [
javascript_inline_importmap_tag,
javascript_importmap_module_preload_tags,
javascript_inline_importmap_tag(Rails.application.importmap.to_json(resolver: self, cache_key: inline_importmap_cache_key)),
javascript_importmap_module_preload_tags(cache_key: module_preload_tags_cache_key),
(javascript_importmap_shim_nonce_configuration_tag if shim),
(javascript_importmap_shim_tag if shim),
javascript_import_module_tag(entry_point)
Expand Down Expand Up @@ -34,15 +34,15 @@ def javascript_importmap_shim_tag(minimized: true)
# Import a named JavaScript module(s) using a script-module tag.
def javascript_import_module_tag(*module_names)
imports = Array(module_names).collect { |m| %(import "#{m}") }.join("\n")
tag.script imports.html_safe,
tag.script imports.html_safe,
type: "module", nonce: request&.content_security_policy_nonce
end

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

# Link tag(s) for preloading the JavaScript module residing in `*paths`. Will return one link tag per path element.
Expand Down
24 changes: 24 additions & 0 deletions test/importmap_tags_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,28 @@ def content_security_policy_nonce
ensure
@request = nil
end

test "separate caches for inline importmap" do
set_one = javascript_importmap_tags(inline_importmap_cache_key: 'inline-1')
ActionController::Base.asset_host = "http://assets.example.com"

set_two = javascript_importmap_tags(inline_importmap_cache_key: 'inline-2')

assert_not_equal set_one, set_two
assert_match /assets\.example\.com/, set_two
ensure
ActionController::Base.asset_host = nil
end

test "separate caches for preloaded modules" do
set_one = javascript_importmap_tags(module_preload_tags_cache_key: 'preload-1')
ActionController::Base.asset_host = "http://assets.example.com"

set_two = javascript_importmap_tags(module_preload_tags_cache_key: 'preload-2')

assert_not_equal set_one, set_two
assert_match /assets\.example\.com/, set_two
ensure
ActionController::Base.asset_host = nil
end
end