Skip to content

Commit 199071c

Browse files
Fix bug at resolving react-server-client-manifest.json file (#1818)
* Enhance server bundle handling by adding support for react_server_client_manifest_file in server_bundle? method and refactoring react_server_manifest_path assignment to use bundle_js_file_path. This improves configuration flexibility and maintains consistency in bundle management. * Fix failing tests after react_server_client_manifest_file changes - Add react_server_client_manifest_file parameter to mock_bundle_configs helper - Update react_server_client_manifest_file_path tests to mock bundle_js_file_path instead of public_bundles_full_path - Ensure trailing newline in spec file These changes fix the CI failures introduced by using bundle_js_file_path for manifest resolution. Co-authored-by: Abanoub Ghadban <[email protected]> * Revert "Fix failing tests after react_server_client_manifest_file changes" This reverts commit 81c385f. * Enhance tests for react_server_client_manifest_file_path - Added support for react_server_client_manifest_file in tests. - Updated test cases to mock bundle_js_file_path instead of public_bundles_full_path. - Improved caching behavior tests for development and non-development environments. - Ensured proper error handling when manifest file name is nil. These changes improve test coverage and reliability for the react_server_client_manifest_file_path method. --------- Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Abanoub Ghadban <[email protected]>
1 parent 1204d15 commit 199071c

File tree

2 files changed

+31
-68
lines changed

2 files changed

+31
-68
lines changed

lib/react_on_rails/utils.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ def self.bundle_js_file_path(bundle_name)
112112
private_class_method def self.server_bundle?(bundle_name)
113113
config = ReactOnRails.configuration
114114
bundle_name == config.server_bundle_js_file ||
115-
bundle_name == config.rsc_bundle_js_file
115+
bundle_name == config.rsc_bundle_js_file ||
116+
bundle_name == config.react_server_client_manifest_file
116117
end
117118

118119
private_class_method def self.handle_missing_manifest_entry(bundle_name, is_server_bundle)
@@ -170,7 +171,7 @@ def self.react_server_client_manifest_file_path
170171
"react_server_client_manifest_file is nil, ensure it is set in your configuration"
171172
end
172173

173-
@react_server_manifest_path = File.join(public_bundles_full_path, asset_name)
174+
@react_server_manifest_path = bundle_js_file_path(asset_name)
174175
end
175176

176177
def self.running_on_windows?

spec/react_on_rails/utils_spec.rb

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ def mock_dev_server_running
9898
.and_return("server-bundle.js")
9999
allow(ReactOnRails).to receive_message_chain("configuration.rsc_bundle_js_file")
100100
.and_return("rsc-bundle.js")
101+
allow(ReactOnRails).to receive_message_chain("configuration.react_server_client_manifest_file")
102+
.and_return("react-server-client-manifest.json")
101103
end
102104

103105
subject do
@@ -786,90 +788,50 @@ def mock_dev_server_running
786788
end
787789

788790
describe ".react_server_client_manifest_file_path" do
791+
let(:asset_name) { "react-server-client-manifest.json" }
792+
789793
before do
790794
described_class.instance_variable_set(:@react_server_manifest_path, nil)
791-
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
792-
.and_return("react-server-client-manifest.json")
795+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file).and_return(asset_name)
793796
allow(Rails.env).to receive(:development?).and_return(false)
794797
end
795798

796799
after do
797800
described_class.instance_variable_set(:@react_server_manifest_path, nil)
798801
end
799802

800-
context "when in development environment" do
801-
before do
802-
allow(Rails.env).to receive(:development?).and_return(true)
803-
allow(described_class).to receive(:public_bundles_full_path)
804-
.and_return("/path/to/generated/assets")
805-
end
806-
807-
it "does not use cached path" do
808-
# Call once to potentially set the cached path
809-
described_class.react_server_client_manifest_file_path
810-
811-
# Change the configuration value
812-
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
813-
.and_return("changed-manifest.json")
814-
815-
# Should use the new value
816-
expect(described_class.react_server_client_manifest_file_path)
817-
.to eq("/path/to/generated/assets/changed-manifest.json")
818-
end
803+
it "calls bundle_js_file_path with the correct asset name and returns its value" do
804+
allow(described_class).to receive(:bundle_js_file_path).with(asset_name).and_return("/some/path/#{asset_name}")
805+
result = described_class.react_server_client_manifest_file_path
806+
expect(described_class).to have_received(:bundle_js_file_path).with(asset_name)
807+
expect(result).to eq("/some/path/#{asset_name}")
819808
end
820809

821-
context "when not in development environment" do
822-
before do
823-
allow(described_class).to receive(:public_bundles_full_path)
824-
.and_return("/path/to/generated/assets")
825-
end
826-
827-
it "caches the path" do
828-
# Call once to set the cached path
829-
expected_path = "/path/to/generated/assets/react-server-client-manifest.json"
830-
expect(described_class.react_server_client_manifest_file_path).to eq(expected_path)
831-
832-
# Change the configuration value
833-
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
834-
.and_return("changed-manifest.json")
835-
836-
# Should still use the cached path
837-
expect(described_class.react_server_client_manifest_file_path).to eq(expected_path)
838-
end
810+
it "caches the path when not in development" do
811+
allow(described_class).to receive(:bundle_js_file_path).with(asset_name).and_return("/some/path/#{asset_name}")
812+
result1 = described_class.react_server_client_manifest_file_path
813+
result2 = described_class.react_server_client_manifest_file_path
814+
expect(described_class).to have_received(:bundle_js_file_path).once.with(asset_name)
815+
expect(result1).to eq("/some/path/#{asset_name}")
816+
expect(result2).to eq("/some/path/#{asset_name}")
839817
end
840818

841-
context "with different manifest file names" do
842-
before do
843-
allow(described_class).to receive(:public_bundles_full_path)
844-
.and_return("/path/to/generated/assets")
845-
end
846-
847-
it "returns the correct path for default manifest name" do
848-
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
849-
.and_return("react-server-client-manifest.json")
850-
851-
expect(described_class.react_server_client_manifest_file_path)
852-
.to eq("/path/to/generated/assets/react-server-client-manifest.json")
853-
end
854-
855-
it "returns the correct path for custom manifest name" do
856-
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
857-
.and_return("custom-server-client-manifest.json")
858-
859-
expect(described_class.react_server_client_manifest_file_path)
860-
.to eq("/path/to/generated/assets/custom-server-client-manifest.json")
861-
end
819+
it "does not cache the path in development" do
820+
allow(Rails.env).to receive(:development?).and_return(true)
821+
allow(described_class).to receive(:bundle_js_file_path).with(asset_name).and_return("/some/path/#{asset_name}")
822+
result1 = described_class.react_server_client_manifest_file_path
823+
result2 = described_class.react_server_client_manifest_file_path
824+
expect(described_class).to have_received(:bundle_js_file_path).twice.with(asset_name)
825+
expect(result1).to eq("/some/path/#{asset_name}")
826+
expect(result2).to eq("/some/path/#{asset_name}")
862827
end
863828

864-
context "with nil manifest file name" do
829+
context "when manifest file name is nil" do
865830
before do
866-
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
867-
.and_return(nil)
868-
allow(described_class).to receive(:public_bundles_full_path)
869-
.and_return("/path/to/generated/assets")
831+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file).and_return(nil)
870832
end
871833

872-
it "raises an error when the manifest file name is nil" do
834+
it "raises an error" do
873835
expect { described_class.react_server_client_manifest_file_path }
874836
.to raise_error(ReactOnRails::Error, /react_server_client_manifest_file is nil/)
875837
end

0 commit comments

Comments
 (0)