Skip to content

Commit 1c811f3

Browse files
committed
Fix using javascript_importmap_tag in console
Previously this would error because there is no request object in the console for `content_security_policy_nonce` to be called on.
1 parent d16f6c0 commit 1c811f3

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

app/helpers/importmap/importmap_tags_helper.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def javascript_importmap_tags(entry_point = "application", shim: true)
1414
# By default, `Rails.application.importmap.to_json(resolver: self)` is used.
1515
def javascript_inline_importmap_tag(importmap_json = Rails.application.importmap.to_json(resolver: self))
1616
tag.script importmap_json.html_safe,
17-
type: "importmap", "data-turbo-track": "reload", nonce: content_security_policy_nonce
17+
type: "importmap", "data-turbo-track": "reload", nonce: request&.content_security_policy
1818
end
1919

2020
# Configure es-modules-shim with nonce support if the application is using a content security policy.
2121
def javascript_importmap_shim_nonce_configuration_tag
22-
if content_security_policy?
22+
if request&.content_security_policy
2323
tag.script({ nonce: content_security_policy_nonce }.to_json.html_safe,
2424
type: "esms-options", nonce: content_security_policy_nonce)
2525
end
@@ -28,14 +28,14 @@ def javascript_importmap_shim_nonce_configuration_tag
2828
# Include the es-modules-shim needed to make importmaps work in browsers without native support (like Firefox + Safari).
2929
def javascript_importmap_shim_tag(minimized: true)
3030
javascript_include_tag minimized ? "es-module-shims.min.js" : "es-module-shims.js",
31-
async: true, "data-turbo-track": "reload", nonce: content_security_policy_nonce
31+
async: true, "data-turbo-track": "reload", nonce: request&.content_security_policy
3232
end
3333

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")
3737
tag.script imports.html_safe,
38-
type: "module", nonce: content_security_policy_nonce
38+
type: "module", nonce: request&.content_security_policy
3939
end
4040

4141
# Link tags for preloading all modules marked as preload: true in the `importmap`
@@ -48,7 +48,7 @@ def javascript_importmap_module_preload_tags(importmap = Rails.application.impor
4848
# Link tag(s) for preloading the JavaScript module residing in `*paths`. Will return one link tag per path element.
4949
def javascript_module_preload_tag(*paths)
5050
safe_join(Array(paths).collect { |path|
51-
tag.link rel: "modulepreload", href: path, nonce: content_security_policy_nonce
51+
tag.link rel: "modulepreload", href: path, nonce: request&.content_security_policy
5252
}, "\n")
5353
end
5454
end

test/importmap_tags_helper_test.rb

-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
require "test_helper"
22

3-
# Stub method
4-
def content_security_policy_nonce() nil end
5-
def content_security_policy?() false end
6-
73
class Importmap::ImportmapTagsHelperTest < ActionView::TestCase
84
test "javascript_importmap_tags with and without shim" do
95
assert_match /shim/, javascript_importmap_tags("application")

0 commit comments

Comments
 (0)