Skip to content

Commit e750328

Browse files
refactor: Return configured server_bundle_output_path directly without existence check
When server_bundle_output_path is configured, the bundle path resolution now returns the configured path immediately without checking if the file exists. This ensures predictable behavior and allows the server rendering process to handle missing files appropriately. Updates: - Modified bundle_js_file_path_with_packer to return configured path directly - Removed try_server_bundle_output_path method (no longer needed) - Updated tests to reflect new behavior without File.exist? checks Co-authored-by: Abanoub Ghadban <[email protected]>
1 parent 3666793 commit e750328

File tree

2 files changed

+15
-50
lines changed

2 files changed

+15
-50
lines changed

lib/react_on_rails/utils.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ def self.bundle_js_file_path(bundle_name)
8888

8989
is_server_bundle = server_bundle?(bundle_name)
9090
config = ReactOnRails.configuration
91+
root_path = Rails.root || "."
9192

92-
# If server bundle and server_bundle_output_path is configured, try that first
93+
# If server bundle and server_bundle_output_path is configured, return that path directly
9394
if is_server_bundle && config.server_bundle_output_path.present?
94-
server_path = try_server_bundle_output_path(bundle_name)
95-
return server_path if server_path
95+
return File.expand_path(File.join(root_path, config.server_bundle_output_path, bundle_name))
9696
end
9797

9898
# Try manifest lookup for all bundles
@@ -109,15 +109,6 @@ def self.bundle_js_file_path(bundle_name)
109109
bundle_name == config.rsc_bundle_js_file
110110
end
111111

112-
private_class_method def self.try_server_bundle_output_path(bundle_name)
113-
config = ReactOnRails.configuration
114-
root_path = Rails.root || "."
115-
116-
# Try the configured server_bundle_output_path
117-
path = File.join(root_path, config.server_bundle_output_path, bundle_name)
118-
expanded_path = File.expand_path(path)
119-
File.exist?(expanded_path) ? expanded_path : nil
120-
end
121112

122113
private_class_method def self.handle_missing_manifest_entry(bundle_name, is_server_bundle)
123114
config = ReactOnRails.configuration

spec/react_on_rails/utils_spec.rb

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,9 @@ def mock_dev_server_running
169169
.and_return("ssr-generated")
170170
end
171171

172-
it "tries configured location first for server bundles" do
173-
allow(File).to receive(:exist?).and_call_original
174-
allow(File).to receive(:exist?).with(ssr_generated_path).and_return(true)
175-
176-
result = described_class.bundle_js_file_path(server_bundle_name)
177-
expect(result).to eq(ssr_generated_path)
178-
end
179-
180-
it "falls back to configured path when no bundle exists" do
181-
allow(File).to receive(:exist?).and_call_original
182-
allow(File).to receive(:exist?).and_return(false)
172+
it "returns configured path directly without checking existence" do
173+
# Should not check File.exist? - returns path immediately
174+
expect(File).not_to receive(:exist?)
183175

184176
result = described_class.bundle_js_file_path(server_bundle_name)
185177
expect(result).to eq(ssr_generated_path)
@@ -214,9 +206,9 @@ def mock_dev_server_running
214206
.and_return("ssr-generated")
215207
end
216208

217-
it "treats RSC bundles as server bundles and tries configured location first" do
218-
allow(File).to receive(:exist?).and_call_original
219-
allow(File).to receive(:exist?).with(ssr_generated_path).and_return(true)
209+
it "treats RSC bundles as server bundles and returns configured path directly" do
210+
# Should not check File.exist? - returns path immediately
211+
expect(File).not_to receive(:exist?)
220212

221213
result = described_class.bundle_js_file_path(rsc_bundle_name)
222214
expect(result).to eq(ssr_generated_path)
@@ -278,33 +270,15 @@ def mock_dev_server_running
278270
expect(path).to end_with("ssr-generated/#{server_bundle_name}")
279271
end
280272

281-
context "with bundle file existing in ssr-generated location" do
282-
it "returns the ssr-generated location path" do
283-
server_bundle_name = "server-bundle.js"
284-
mock_bundle_configs(server_bundle_name: server_bundle_name)
285-
mock_missing_manifest_entry(server_bundle_name)
286-
287-
# Mock File.exist? to return true for ssr-generated path
288-
ssr_generated_path = File.expand_path(File.join("ssr-generated", server_bundle_name))
289-
290-
allow(File).to receive(:exist?).and_call_original
291-
allow(File).to receive(:exist?).with(ssr_generated_path).and_return(true)
292-
293-
path = described_class.server_bundle_js_file_path
294-
295-
expect(path).to eq(ssr_generated_path)
296-
end
297-
end
298-
299-
context "with bundle file not existing in any fallback location" do
300-
it "returns the secure ssr-generated path as final fallback for server bundles" do
273+
context "with server_bundle_output_path configured" do
274+
it "returns the configured path directly without checking file existence" do
301275
server_bundle_name = "server-bundle.js"
302276
mock_bundle_configs(server_bundle_name: server_bundle_name)
303-
mock_missing_manifest_entry(server_bundle_name)
304277

305-
# Mock File.exist? to return false for all paths
306-
allow(File).to receive(:exist?).and_call_original
307-
allow(File).to receive(:exist?).and_return(false)
278+
# Since server_bundle_output_path is configured, should return path immediately
279+
# without trying manifest lookup
280+
expect(ReactOnRails::PackerUtils.packer).not_to receive(:manifest)
281+
expect(File).not_to receive(:exist?)
308282

309283
path = described_class.server_bundle_js_file_path
310284

0 commit comments

Comments
 (0)