From 5a5918795799aff13140d2a8f20fff272f13e983 Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Sun, 19 Feb 2023 12:15:49 +0100 Subject: [PATCH 1/2] Accept Cache Key for Inline & Preloaded Module in Importmap Tag --- Gemfile.lock | 1 + .../importmap/importmap_tags_helper.rb | 12 +++++----- test/importmap_tags_helper_test.rb | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index df3b7e6..b756d6b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/helpers/importmap/importmap_tags_helper.rb b/app/helpers/importmap/importmap_tags_helper.rb index ce3da83..527b6a5 100644 --- a/app/helpers/importmap/importmap_tags_helper.rb +++ b/app/helpers/importmap/importmap_tags_helper.rb @@ -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) @@ -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. diff --git a/test/importmap_tags_helper_test.rb b/test/importmap_tags_helper_test.rb index b28bb81..9a9c4b6 100644 --- a/test/importmap_tags_helper_test.rb +++ b/test/importmap_tags_helper_test.rb @@ -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/, javascript_importmap_tags(set_one) + 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/, javascript_importmap_tags(set_one) + ensure + ActionController::Base.asset_host = nil + end end From 68163ec52b6351022619c9213cd8ec610ff55681 Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Mon, 20 Feb 2023 15:39:14 +0100 Subject: [PATCH 2/2] Fixing Tests --- test/importmap_tags_helper_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/importmap_tags_helper_test.rb b/test/importmap_tags_helper_test.rb index 9a9c4b6..6a08813 100644 --- a/test/importmap_tags_helper_test.rb +++ b/test/importmap_tags_helper_test.rb @@ -63,7 +63,7 @@ def content_security_policy_nonce set_two = javascript_importmap_tags(inline_importmap_cache_key: 'inline-2') assert_not_equal set_one, set_two - assert_match /assets\.example\.com/, javascript_importmap_tags(set_one) + assert_match /assets\.example\.com/, set_two ensure ActionController::Base.asset_host = nil end @@ -75,7 +75,7 @@ def content_security_policy_nonce set_two = javascript_importmap_tags(module_preload_tags_cache_key: 'preload-2') assert_not_equal set_one, set_two - assert_match /assets\.example\.com/, javascript_importmap_tags(set_one) + assert_match /assets\.example\.com/, set_two ensure ActionController::Base.asset_host = nil end