Module: ReactOnRailsPro::ServerRenderingJsCode

Defined in:
lib/react_on_rails_pro/server_rendering_js_code.rb

Class Method Summary collapse

Class Method Details

.generate_rsc_payload_js_function(render_options) ⇒ String

Generates the JavaScript function used for React Server Components payload generation Returns the JavaScript code that defines the generateRSCPayload function. It also adds necessary information to the railsContext to generate the RSC payload for any component in the app.

Returns:

  • (String)

    JavaScript code for RSC payload generation



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/react_on_rails_pro/server_rendering_js_code.rb', line 14

def generate_rsc_payload_js_function(render_options)
  return "" unless ReactOnRailsPro.configuration.enable_rsc_support && render_options.streaming?

  if render_options.rsc_payload_streaming?
    # When already on RSC bundle, we prevent further RSC payload generation
    # by throwing an error if generateRSCPayload is called
    return "      if (typeof generateRSCPayload !== 'function') {\n        globalThis.generateRSCPayload = function generateRSCPayload() {\n          throw new Error('The rendering request is already running on the RSC bundle. Please ensure that generateRSCPayload is only called from any React Server Component.')\n        }\n      }\n    JS\n  end\n\n  # To minimize the size of the HTTP request body sent to the node renderer,\n  # we reuse the existing rendering request string within the generateRSCPayload function.\n  # This approach allows us to simply replace the component name and props,\n  # rather than rewriting the entire rendering request.\n  # This regex finds the empty function call pattern `()` and replaces it with the component and props\n  <<-JS\n  railsContext.serverSideRSCPayloadParameters = {\n    renderingRequest,\n    rscBundleHash: '\#{ReactOnRailsPro::Utils.rsc_bundle_hash}',\n  }\n  if (typeof generateRSCPayload !== 'function') {\n    globalThis.generateRSCPayload = function generateRSCPayload(componentName, props, railsContext) {\n      const { renderingRequest, rscBundleHash } = railsContext.serverSideRSCPayloadParameters;\n      const propsString = JSON.stringify(props);\n      const newRenderingRequest = renderingRequest.replace(/\\\\(\\\\s*\\\\)\\\\s*$/, `('${componentName}', ${propsString})`);\n      return runOnOtherBundle(rscBundleHash, newRenderingRequest);\n    }\n  }\n  JS\nend\n"

.render(props_string, rails_context, redux_stores, react_component_name, render_options) ⇒ String

Main rendering function that generates JavaScript code for server-side rendering

Parameters:

  • props_string (String)

    JSON string of props to pass to the React component

  • rails_context (String)

    JSON string of Rails context data

  • redux_stores (String)

    JavaScript code for Redux stores initialization

  • react_component_name (String)

    Name of the React component to render

  • render_options (Object)

    Options that control the rendering behavior

Returns:

  • (String)

    JavaScript code that will render the React component on the server



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/react_on_rails_pro/server_rendering_js_code.rb', line 57

def render(props_string, rails_context, redux_stores, react_component_name, render_options)
  render_function_name =
    if ReactOnRailsPro.configuration.enable_rsc_support && render_options.streaming?
      # Select appropriate function based on whether the rendering request is running on server or rsc bundle
      # As the same rendering request is used to generate the rsc payload and SSR the component.
      "ReactOnRails.isRSCBundle ? 'serverRenderRSCReactComponent' : 'streamServerRenderedReactComponent'"
    else
      "'serverRenderReactComponent'"
    end
  rsc_params = if ReactOnRailsPro.configuration.enable_rsc_support && render_options.streaming?
                 config = ReactOnRailsPro.configuration
                 react_client_manifest_file = config.react_client_manifest_file
                 react_server_client_manifest_file = config.react_server_client_manifest_file
                 "                    railsContext.reactClientManifestFileName = '\#{react_client_manifest_file}';\n                    railsContext.reactServerClientManifestFileName = '\#{react_server_client_manifest_file}';\n                 JS\n               else\n                 \"\"\n               end\n\n  # This function is called with specific componentName and props when generateRSCPayload is invoked\n  # In that case, it replaces the empty () with ('componentName', props) in the rendering request\n  <<-JS\n  (function(componentName = '\#{react_component_name}', props = undefined) {\n    var railsContext = \#{rails_context};\n    \#{rsc_params}\n    \#{generate_rsc_payload_js_function(render_options)}\n    \#{ssr_pre_hook_js}\n    \#{redux_stores}\n    var usedProps = typeof props === 'undefined' ? \#{props_string} : props;\n    return ReactOnRails[\#{render_function_name}]({\n      name: componentName,\n      domNodeId: '\#{render_options.dom_id}',\n      props: usedProps,\n      trace: \#{render_options.trace},\n      railsContext: railsContext,\n      throwJsErrors: \#{ReactOnRailsPro.configuration.throw_js_errors},\n      renderingReturnsPromises: \#{ReactOnRailsPro.configuration.rendering_returns_promises},\n    });\n  })()\n  JS\nend\n"

.ssr_pre_hook_jsObject



6
7
8
# File 'lib/react_on_rails_pro/server_rendering_js_code.rb', line 6

def ssr_pre_hook_js
  ReactOnRailsPro.configuration.ssr_pre_hook_js || ""
end