Skip to content

Commit d06d51f

Browse files
authored
[codex] Fix Propshaft asset version cache busting (#1403)
* Fix Propshaft asset version cache busting * Add cache busting edge case coverage
1 parent fb419fb commit d06d51f

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

lib/react/rails/railtie.rb

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ class Railtie < ::Rails::Railtie
7676
variant: app.config.react.variant
7777
})
7878

79-
sprockets_env = app.assets || app.config.try(:assets) # sprockets-rails 3.x attaches this at a different config
80-
unless sprockets_env.nil?
81-
sprockets_env.version = [sprockets_env.version, "react-#{asset_variant.react_build}"].compact.join("-")
82-
end
79+
assets = app.assets || app.config.try(:assets) # sprockets-rails 3.x attaches this at a different config
80+
Railtie.append_react_build_to_assets_version!(assets, asset_variant.react_build)
8381
end
8482

8583
initializer "react_rails.set_variant", after: :engines_blank_point, group: :all do |app|
@@ -114,6 +112,29 @@ class Railtie < ::Rails::Railtie
114112
React::JSX::SprocketsStrategy.attach_with_strategy(sprockets_env, app.config.react.sprockets_strategy)
115113
end
116114
end
115+
116+
# :nodoc:
117+
def self.append_react_build_to_assets_version!(assets, react_build)
118+
versioned_assets = versioned_assets_for(assets)
119+
return if versioned_assets.nil?
120+
121+
versioned_assets.version = [versioned_assets.version, "react-#{react_build}"].compact.join("-")
122+
end
123+
124+
def self.versioned_assets_for(assets)
125+
return assets if versioned_assets?(assets)
126+
127+
config = assets.config if assets.respond_to?(:config)
128+
return config if versioned_assets?(config)
129+
130+
nil
131+
end
132+
133+
def self.versioned_assets?(assets)
134+
assets.respond_to?(:version) && assets.respond_to?(:version=)
135+
end
136+
137+
private_class_method :versioned_assets_for, :versioned_assets?
117138
end
118139
end
119140
end

test/react/rails/railtie_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,46 @@
33
require "test_helper"
44

55
class RailtieTest < ActionDispatch::IntegrationTest
6+
VersionedAssets = Struct.new(:version)
7+
PropshaftLikeAssembly = Struct.new(:config)
8+
69
test "reloaders are configured after initializers are loaded" do
710
@test_file = File.expand_path("../../dummy/app/pants/yfronts.js", File.dirname(__FILE__))
811
FileUtils.touch @test_file
912
results = Dummy::Application.reloaders.map(&:updated?)
1013

1114
assert_includes(results, true)
1215
end
16+
17+
test "cache busting updates asset environments with a direct version" do
18+
assets = VersionedAssets.new("1.0")
19+
20+
React::Rails::Railtie.append_react_build_to_assets_version!(assets, "development")
21+
22+
assert_equal "1.0-react-development", assets.version
23+
end
24+
25+
test "cache busting updates asset assembly configs with a version" do
26+
config = ActiveSupport::OrderedOptions.new
27+
config.version = "1.0"
28+
assembly = PropshaftLikeAssembly.new(config)
29+
30+
React::Rails::Railtie.append_react_build_to_assets_version!(assembly, "production")
31+
32+
assert_equal "1.0-react-production", config.version
33+
end
34+
35+
test "cache busting is a no-op when assets is nil" do
36+
assert_nothing_raised do
37+
React::Rails::Railtie.append_react_build_to_assets_version!(nil, "development")
38+
end
39+
end
40+
41+
test "cache busting is a no-op when assets has no version" do
42+
assets = Object.new
43+
44+
assert_nothing_raised do
45+
React::Rails::Railtie.append_react_build_to_assets_version!(assets, "development")
46+
end
47+
end
1348
end

0 commit comments

Comments
 (0)