From 11d153ef70c567f7d297dc29b6a814bdfdcbc2a5 Mon Sep 17 00:00:00 2001 From: Mitchell Henke Date: Mon, 25 Mar 2024 12:39:27 -0500 Subject: [PATCH 1/3] Refactor ScriptHelper class to be thread-safe changelog: Internal, Performance, Refactor ScriptHelper class to be thread-safe --- app/helpers/script_helper.rb | 13 +++++++++---- spec/rails_helper.rb | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/helpers/script_helper.rb b/app/helpers/script_helper.rb index 54171c9d7d5..84ba43cecc0 100644 --- a/app/helpers/script_helper.rb +++ b/app/helpers/script_helper.rb @@ -3,7 +3,12 @@ # 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] + if scripts + RequestStore.store[:scripts].merge!(names.index_with(attributes)) + else + RequestStore.store[:scripts] = names.index_with(attributes) + end nil end @@ -12,9 +17,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, @@ -42,7 +47,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)) } diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 04b0c431cdd..3bb6446ceb6 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -121,6 +121,7 @@ class Analytics end config.before(:each) do + RequestStore.clear! Rails.cache.clear end From d06fb774ae4f6b4f79923e3872b8d8072f8b8eba Mon Sep 17 00:00:00 2001 From: Mitchell Henke Date: Mon, 25 Mar 2024 13:22:03 -0500 Subject: [PATCH 2/3] add request_store to Gemfile --- Gemfile | 1 + Gemfile.lock | 1 + 2 files changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index c89b14189e1..2a4e40422db 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index d0258b2095f..0b726685692 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -835,6 +835,7 @@ DEPENDENCIES redacted_struct redis (>= 3.2.0) redis-session-store! + request_store (~> 1.0) retries rotp (~> 6.1) rqrcode From ee1ad6a338a5c367057554356c92768f80abf68c Mon Sep 17 00:00:00 2001 From: Mitchell Henke Date: Mon, 25 Mar 2024 15:11:29 -0500 Subject: [PATCH 3/3] remove rubocop exception --- app/helpers/script_helper.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/helpers/script_helper.rb b/app/helpers/script_helper.rb index 84ba43cecc0..885f19df59d 100644 --- a/app/helpers/script_helper.rb +++ b/app/helpers/script_helper.rb @@ -1,6 +1,5 @@ # frozen_string_literal: true -# rubocop:disable Rails/HelperInstanceVariable module ScriptHelper def javascript_packs_tag_once(*names, **attributes) scripts = RequestStore.store[:scripts] @@ -72,4 +71,3 @@ def asset_host(path) end end end -# rubocop:enable Rails/HelperInstanceVariable