Skip to content
Draft
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
28 changes: 24 additions & 4 deletions lib/react/rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ class Railtie < ::Rails::Railtie
variant: app.config.react.variant
})

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

initializer "react_rails.set_variant", after: :engines_blank_point, group: :all do |app|
Expand Down Expand Up @@ -114,6 +112,28 @@ class Railtie < ::Rails::Railtie
React::JSX::SprocketsStrategy.attach_with_strategy(sprockets_env, app.config.react.sprockets_strategy)
end
end

def self.append_react_build_to_assets_version!(assets, react_build)
versioned_assets = versioned_assets_for(assets)
return if versioned_assets.nil?

versioned_assets.version = [versioned_assets.version, "react-#{react_build}"].compact.join("-")
end

def self.versioned_assets_for(assets)
return assets if versioned_assets?(assets)

config = assets.config if assets.respond_to?(:config)
return config if versioned_assets?(config)

nil
end

def self.versioned_assets?(assets)
assets&.respond_to?(:version) && assets.respond_to?(:version=)
end

private_class_method :versioned_assets_for, :versioned_assets?
end
end
end
21 changes: 21 additions & 0 deletions test/react/rails/railtie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@
require "test_helper"

class RailtieTest < ActionDispatch::IntegrationTest
VersionedAssets = Struct.new(:version)
PropshaftLikeAssembly = Struct.new(:config)

test "reloaders are configured after initializers are loaded" do
@test_file = File.expand_path("../../dummy/app/pants/yfronts.js", File.dirname(__FILE__))
FileUtils.touch @test_file
results = Dummy::Application.reloaders.map(&:updated?)

assert_includes(results, true)
end

test "cache busting updates asset environments with a direct version" do
assets = VersionedAssets.new("1.0")

React::Rails::Railtie.append_react_build_to_assets_version!(assets, "development")

assert_equal "1.0-react-development", assets.version
end

test "cache busting updates asset assembly configs with a version" do
config = ActiveSupport::OrderedOptions.new
config.version = "1.0"
assembly = PropshaftLikeAssembly.new(config)

React::Rails::Railtie.append_react_build_to_assets_version!(assembly, "production")

assert_equal "1.0-react-production", config.version
end
end
Loading