Skip to content

Commit 0a36833

Browse files
add missing docs and specs
1 parent 552efd2 commit 0a36833

File tree

5 files changed

+218
-2
lines changed

5 files changed

+218
-2
lines changed

docs/guides/configuration.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ ReactOnRails.configure do |config|
104104
# you should include a name that matches your bundle name in your Webpack config.
105105
config.server_bundle_js_file = "server-bundle.js"
106106

107+
# When using React on Rails Pro with RSC support enabled, these configuration options work together:
108+
#
109+
# 1. In RORP, set `config.enable_rsc_support = true` in your react_on_rails_pro.rb initializer
110+
#
111+
# 2. The `rsc_bundle_js_file` (typically "rsc-bundle.js") contains only server components and
112+
# references to client components. It's generated using the RSC Webpack Loader which transforms
113+
# client components into references. This bundle is specifically used for generating RSC payloads
114+
# and is configured with the `react-server` condition.
115+
config.rsc_bundle_js_file = "rsc-bundle.js"
116+
#
117+
# 3. The `react_client_manifest_file` contains mappings for client components that need hydration.
118+
# It's generated by the React Server Components Webpack plugin and is required for client-side
119+
# hydration of components.
120+
# This manifest file is automatically generated by the React Server Components Webpack plugin. Only set this if you've configured the plugin to use a different filename.
121+
config.react_client_manifest_file = "react-client-manifest.json"
122+
#
123+
# 4. The `react_server_client_manifest_file` is used during server-side rendering with RSC to
124+
# properly resolve references between server and client components.
125+
#
126+
# These files are crucial when implementing React Server Components with streaming, which offers
127+
# benefits like reduced JavaScript bundle sizes, faster page loading, and selective hydration
128+
# of client components.
129+
# This manifest file is automatically generated by the React Server Components Webpack plugin. Only set this if you've configured the plugin to use a different filename.
130+
config.react_server_client_manifest_file = "react-server-client-manifest.json"
131+
107132
# `prerender` means server-side rendering
108133
# default is false. This is an option for view helpers `render_component` and `render_component_hash`.
109134
# Set to true to change the default value to true.

lib/react_on_rails/configuration.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,6 @@ def ensure_webpack_generated_files_exists
312312
react_client_manifest_file,
313313
react_server_client_manifest_file
314314
].compact_blank
315-
316-
self.webpack_generated_files = files
317315
end
318316

319317
def configure_skip_display_none_deprecation

lib/react_on_rails/utils.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def self.react_server_client_manifest_file_path
128128
return @react_server_manifest_path if @react_server_manifest_path && !Rails.env.development?
129129

130130
asset_name = ReactOnRails.configuration.react_server_client_manifest_file
131+
if asset_name.nil?
132+
raise ReactOnRails::Error,
133+
"react_server_client_manifest_file is nil, ensure to set it in your configuration"
134+
end
135+
131136
@react_server_manifest_path = File.join(generated_assets_full_path, asset_name)
132137
end
133138

spec/react_on_rails/configuration_spec.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,103 @@ module ReactOnRails
224224
end
225225
end
226226

227+
describe "RSC configuration options" do
228+
it "has default values for RSC-related configuration options" do
229+
ReactOnRails.configure {} # rubocop:disable Lint/EmptyBlock
230+
231+
expect(ReactOnRails.configuration.rsc_bundle_js_file).to eq("")
232+
expect(ReactOnRails.configuration.react_client_manifest_file).to eq("react-client-manifest.json")
233+
expect(ReactOnRails.configuration.react_server_client_manifest_file).to eq("react-server-client-manifest.json")
234+
end
235+
236+
it "allows setting rsc_bundle_js_file" do
237+
ReactOnRails.configure do |config|
238+
config.rsc_bundle_js_file = "custom-rsc-bundle.js"
239+
end
240+
241+
expect(ReactOnRails.configuration.rsc_bundle_js_file).to eq("custom-rsc-bundle.js")
242+
end
243+
244+
it "allows setting react_client_manifest_file" do
245+
ReactOnRails.configure do |config|
246+
config.react_client_manifest_file = "custom-client-manifest.json"
247+
end
248+
249+
expect(ReactOnRails.configuration.react_client_manifest_file).to eq("custom-client-manifest.json")
250+
end
251+
252+
it "allows setting react_server_client_manifest_file" do
253+
ReactOnRails.configure do |config|
254+
config.react_server_client_manifest_file = "custom-server-client-manifest.json"
255+
end
256+
257+
expect(ReactOnRails.configuration.react_server_client_manifest_file).to eq("custom-server-client-manifest.json")
258+
end
259+
260+
it "includes rsc files in webpack_generated_files when not blank" do
261+
ReactOnRails.configure do |config|
262+
config.rsc_bundle_js_file = "rsc-bundle.js"
263+
config.webpack_generated_files = []
264+
end
265+
266+
expect(ReactOnRails.configuration.webpack_generated_files).to include("rsc-bundle.js")
267+
end
268+
269+
it "includes client manifest in webpack_generated_files" do
270+
ReactOnRails.configure do |config|
271+
config.react_client_manifest_file = "custom-client-manifest.json"
272+
config.webpack_generated_files = []
273+
end
274+
275+
expect(ReactOnRails.configuration.webpack_generated_files).to include("custom-client-manifest.json")
276+
end
277+
278+
it "includes server-client manifest in webpack_generated_files" do
279+
ReactOnRails.configure do |config|
280+
config.react_server_client_manifest_file = "custom-server-client-manifest.json"
281+
config.webpack_generated_files = []
282+
end
283+
284+
expect(ReactOnRails.configuration.webpack_generated_files).to include("custom-server-client-manifest.json")
285+
end
286+
287+
it "configures all RSC options together for a typical RSC setup" do
288+
ReactOnRails.configure do |config|
289+
config.rsc_bundle_js_file = "rsc-bundle.js"
290+
config.react_client_manifest_file = "client-manifest.json"
291+
config.react_server_client_manifest_file = "server-client-manifest.json"
292+
config.webpack_generated_files = []
293+
end
294+
295+
expect(ReactOnRails.configuration.rsc_bundle_js_file).to eq("rsc-bundle.js")
296+
expect(ReactOnRails.configuration.react_client_manifest_file).to eq("client-manifest.json")
297+
expect(ReactOnRails.configuration.react_server_client_manifest_file).to eq("server-client-manifest.json")
298+
299+
# All RSC files should be included in webpack_generated_files
300+
expect(ReactOnRails.configuration.webpack_generated_files).to include("rsc-bundle.js")
301+
expect(ReactOnRails.configuration.webpack_generated_files).to include("client-manifest.json")
302+
expect(ReactOnRails.configuration.webpack_generated_files).to include("server-client-manifest.json")
303+
end
304+
305+
it "allows nil values for RSC configuration options" do
306+
ReactOnRails.configure do |config|
307+
config.rsc_bundle_js_file = nil
308+
config.react_client_manifest_file = nil
309+
config.react_server_client_manifest_file = nil
310+
config.webpack_generated_files = []
311+
end
312+
313+
expect(ReactOnRails.configuration.rsc_bundle_js_file).to be_nil
314+
expect(ReactOnRails.configuration.react_client_manifest_file).to be_nil
315+
expect(ReactOnRails.configuration.react_server_client_manifest_file).to be_nil
316+
317+
# Nil values should not be included in webpack_generated_files
318+
expect(ReactOnRails.configuration.webpack_generated_files).not_to include(nil)
319+
# Only manifest.json should be in the list by default
320+
expect(ReactOnRails.configuration.webpack_generated_files).to eq(["manifest.json"])
321+
end
322+
end
323+
227324
it "changes the configuration of the gem, such as setting the prerender option to false" do
228325
ReactOnRails.configure do |config|
229326
config.server_bundle_js_file = "server.js"

spec/react_on_rails/utils_spec.rb

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,97 @@ def mock_dev_server_running
577577
end
578578
end
579579
end
580+
581+
describe ".react_server_client_manifest_file_path" do
582+
before do
583+
described_class.instance_variable_set(:@react_server_manifest_path, nil)
584+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
585+
.and_return("react-server-client-manifest.json")
586+
allow(Rails.env).to receive(:development?).and_return(false)
587+
end
588+
589+
after do
590+
described_class.instance_variable_set(:@react_server_manifest_path, nil)
591+
end
592+
593+
context "when in development environment" do
594+
before do
595+
allow(Rails.env).to receive(:development?).and_return(true)
596+
allow(described_class).to receive(:generated_assets_full_path)
597+
.and_return("/path/to/generated/assets")
598+
end
599+
600+
it "does not use cached path" do
601+
# Call once to potentially set the cached path
602+
described_class.react_server_client_manifest_file_path
603+
604+
# Change the configuration value
605+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
606+
.and_return("changed-manifest.json")
607+
608+
# Should use the new value
609+
expect(described_class.react_server_client_manifest_file_path)
610+
.to eq("/path/to/generated/assets/changed-manifest.json")
611+
end
612+
end
613+
614+
context "when not in development environment" do
615+
before do
616+
allow(described_class).to receive(:generated_assets_full_path)
617+
.and_return("/path/to/generated/assets")
618+
end
619+
620+
it "caches the path" do
621+
# Call once to set the cached path
622+
expected_path = "/path/to/generated/assets/react-server-client-manifest.json"
623+
expect(described_class.react_server_client_manifest_file_path).to eq(expected_path)
624+
625+
# Change the configuration value
626+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
627+
.and_return("changed-manifest.json")
628+
629+
# Should still use the cached path
630+
expect(described_class.react_server_client_manifest_file_path).to eq(expected_path)
631+
end
632+
end
633+
634+
context "with different manifest file names" do
635+
before do
636+
allow(described_class).to receive(:generated_assets_full_path)
637+
.and_return("/path/to/generated/assets")
638+
end
639+
640+
it "returns the correct path for default manifest name" do
641+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
642+
.and_return("react-server-client-manifest.json")
643+
644+
expect(described_class.react_server_client_manifest_file_path)
645+
.to eq("/path/to/generated/assets/react-server-client-manifest.json")
646+
end
647+
648+
it "returns the correct path for custom manifest name" do
649+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
650+
.and_return("custom-server-client-manifest.json")
651+
652+
expect(described_class.react_server_client_manifest_file_path)
653+
.to eq("/path/to/generated/assets/custom-server-client-manifest.json")
654+
end
655+
end
656+
657+
context "with nil manifest file name" do
658+
before do
659+
allow(ReactOnRails.configuration).to receive(:react_server_client_manifest_file)
660+
.and_return(nil)
661+
allow(described_class).to receive(:generated_assets_full_path)
662+
.and_return("/path/to/generated/assets")
663+
end
664+
665+
it "raises an error when the manifest file name is nil" do
666+
expect { described_class.react_server_client_manifest_file_path }
667+
.to raise_error(ReactOnRails::Error, /react_server_client_manifest_file is nil/)
668+
end
669+
end
670+
end
580671
end
581672
end
582673
# rubocop:enable Metrics/ModuleLength, Metrics/BlockLength

0 commit comments

Comments
 (0)