Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ gem 'rack-timeout', require: false
gem 'redacted_struct'
gem 'redis', '>= 3.2.0'
gem 'redis-session-store', github: '18F/redis-session-store', tag: 'v1.0.1-18f'
gem 'request_store', '~> 1.0'
gem 'retries'
gem 'rotp', '~> 6.1'
gem 'rqrcode'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ DEPENDENCIES
redacted_struct
redis (>= 3.2.0)
redis-session-store!
request_store (~> 1.0)
retries
rotp (~> 6.1)
rqrcode
Expand Down
15 changes: 9 additions & 6 deletions app/helpers/script_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# frozen_string_literal: true

# rubocop:disable Rails/HelperInstanceVariable
module ScriptHelper
def javascript_packs_tag_once(*names, **attributes)
@scripts = @scripts.to_h.merge(names.index_with(attributes))
scripts = RequestStore.store[:scripts]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request_store is only a transitive dependency of lograge, if we're using it directly like this, WDYT of making it an explicit dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yes, meant to do that

Copy link
Contributor Author

@mitchellhenke mitchellhenke Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 14f47f599a

if scripts
RequestStore.store[:scripts].merge!(names.index_with(attributes))
else
RequestStore.store[:scripts] = names.index_with(attributes)
end
Comment on lines 6 to 10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this would be a good case to use the .fetch API from the gem?

Suggested change
if scripts
RequestStore.store[:scripts].merge!(names.index_with(attributes))
else
RequestStore.store[:scripts] = names.index_with(attributes)
end
RequestStore.fetch(:scripts) do
names.index_with(attributes)
end.merge!(names.index_with(attributes))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to use it, but it felt not quite right to merge into itself in the empty case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prob fine to leave as-is

nil
end

Expand All @@ -12,9 +16,9 @@ def javascript_packs_tag_once(*names, **attributes)
def render_javascript_pack_once_tags(...)
capture do
javascript_packs_tag_once(...)
return if @scripts.blank?
return if RequestStore.store[:scripts].blank?
concat javascript_assets_tag
@scripts.each do |name, attributes|
RequestStore.store[:scripts].each do |name, attributes|
asset_sources.get_sources(name).each do |source|
concat javascript_include_tag(
source,
Expand Down Expand Up @@ -42,7 +46,7 @@ def local_crossorigin_sources?
end

def javascript_assets_tag
assets = asset_sources.get_assets(*@scripts.keys)
assets = asset_sources.get_assets(*RequestStore.store[:scripts].keys)

if assets.present?
asset_map = assets.index_with { |path| asset_path(path, host: asset_host(path)) }
Expand All @@ -67,4 +71,3 @@ def asset_host(path)
end
end
end
# rubocop:enable Rails/HelperInstanceVariable
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Analytics
end

config.before(:each) do
RequestStore.clear!
Rails.cache.clear
end

Expand Down