Skip to content

Commit 441c901

Browse files
refactor: make renderRequestId optional in server component rendering for improved flexibility and clarity
1 parent a8d2d1f commit 441c901

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

lib/react_on_rails/react_component/render_options.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def initialize(react_component_name: required("react_component_name"), options:
2121
# Therefore, we need an additional unique identifier that can be used both on the client and server.
2222
# This ID can also be used to associate specific data with a particular rendered component
2323
# on either the server or client.
24-
@render_request_id = self.class.generate_request_id
24+
# This ID is only present if RSC support is enabled because it's only used in that case.
25+
@render_request_id = self.class.generate_request_id if ReactOnRails::Utils.rsc_support_enabled?
2526
end
2627

2728
attr_reader :react_component_name, :render_request_id

lib/react_on_rails/server_rendering_js_code.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def render(props_string, rails_context, redux_stores, react_component_name, rend
3838
<<-JS
3939
(function() {
4040
var railsContext = #{rails_context};
41-
railsContext.componentSpecificMetadata = {renderRequestId: '#{render_options.render_request_id}'};
4241
#{redux_stores}
4342
var props = #{props_string};
4443
return ReactOnRails.serverRenderReactComponent({

node_package/src/ClientSideRenderer.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ class ComponentRenderer {
8585
const trace = el.getAttribute('data-trace') === 'true';
8686
const renderRequestId = el.getAttribute('data-render-request-id');
8787

88-
if (!renderRequestId) {
89-
console.error(`renderRequestId is missing for component ${name} in the DOM node with id ${domNodeId}`);
90-
}
91-
92-
const componentSpecificRailsContext = {
93-
...railsContext,
94-
componentSpecificMetadata: {
95-
renderRequestId: renderRequestId || '',
96-
},
97-
};
88+
// The renderRequestId is optional and only present when React Server Components (RSC) support is enabled.
89+
// When RSC is enabled, this ID helps track and associate server-rendered components with their client-side hydration.
90+
const componentSpecificRailsContext = renderRequestId
91+
? {
92+
...railsContext,
93+
componentSpecificMetadata: {
94+
renderRequestId,
95+
},
96+
}
97+
: railsContext;
9898

9999
try {
100100
const domNode = document.getElementById(domNodeId);

0 commit comments

Comments
 (0)