Class: ReactOnRails::ReactComponent::RenderOptions

Inherits:
Object
  • Object
show all
Includes:
Utils::Required
Defined in:
lib/react_on_rails/react_component/render_options.rb

Constant Summary collapse

PRERENDER_OVERRIDE_ENV_KEY =
"REACT_ON_RAILS_PRERENDER_OVERRIDE"
PRERENDER_OVERRIDE_VALUES =
{ "true" => true, "false" => false }.freeze
PRERENDER_OVERRIDE_CACHE_MUTEX =
Mutex.new
NO_PROPS =
{}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Required

#required

Constructor Details

#initialize(react_component_name: required("react_component_name"), options: required("options")) ⇒ RenderOptions

TODO: remove the required for named params



71
72
73
74
75
# File 'lib/react_on_rails/react_component/render_options.rb', line 71

def initialize(react_component_name: required("react_component_name"), options: required("options"))
  @react_component_name = react_component_name.camelize
  @options = options
  @hydrate_on = ReactComponent.normalize_hydrate_on(options.fetch(:hydrate_on, :immediate))
end

Instance Attribute Details

#hydrate_onObject (readonly)

Returns the value of attribute hydrate_on.



77
78
79
# File 'lib/react_on_rails/react_component/render_options.rb', line 77

def hydrate_on
  @hydrate_on
end

#react_component_nameObject (readonly)

Returns the value of attribute react_component_name.



77
78
79
# File 'lib/react_on_rails/react_component/render_options.rb', line 77

def react_component_name
  @react_component_name
end

#request_digestObject

Returns the value of attribute request_digest.



27
28
29
# File 'lib/react_on_rails/react_component/render_options.rb', line 27

def request_digest
  @request_digest
end

Class Method Details

.generate_request_idObject



198
199
200
# File 'lib/react_on_rails/react_component/render_options.rb', line 198

def self.generate_request_id
  SecureRandom.uuid
end

.prerender_env_overrideObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/react_on_rails/react_component/render_options.rb', line 33

def prerender_env_override
  PRERENDER_OVERRIDE_CACHE_MUTEX.synchronize do
    raw_value = ENV.fetch(PRERENDER_OVERRIDE_ENV_KEY, nil)
    cached_override = @prerender_env_override_cache
    return cached_override[:value] if cached_override && cached_override[:raw_value] == raw_value

    parsed_value = parse_prerender_env_override(raw_value)
    @prerender_env_override_cache = { raw_value:, value: parsed_value }
    parsed_value
  end
end

.reset_prerender_env_override_cache!Object



45
46
47
48
49
# File 'lib/react_on_rails/react_component/render_options.rb', line 45

def reset_prerender_env_override_cache!
  PRERENDER_OVERRIDE_CACHE_MUTEX.synchronize do
    @prerender_env_override_cache = nil
  end
end

Instance Method Details

#auto_load_bundleObject



134
135
136
# File 'lib/react_on_rails/react_component/render_options.rb', line 134

def auto_load_bundle
  retrieve_configuration_value_for(:auto_load_bundle)
end

#client_propsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/react_on_rails/react_component/render_options.rb', line 87

def client_props
  props_extension = ReactOnRails.configuration.rendering_props_extension
  if props_extension.present?
    if props_extension.respond_to?(:adjust_props_for_client_side_hydration)
      return props_extension.adjust_props_for_client_side_hydration(react_component_name,
                                                                    props.clone)
    end

    raise ReactOnRails::Error, "ReactOnRails: your rendering_props_extension module is missing the " \
                               "required adjust_props_for_client_side_hydration method & can not be used"
  end
  props
end

#dom_idObject



105
106
107
108
109
110
111
112
113
# File 'lib/react_on_rails/react_component/render_options.rb', line 105

def dom_id
  @dom_id ||= options.fetch(:id) do
    if random_dom_id
      generate_unique_dom_id
    else
      base_dom_id
    end
  end
end

#html_optionsObject



123
124
125
# File 'lib/react_on_rails/react_component/render_options.rb', line 123

def html_options
  options[:html_options].to_h
end

#html_streaming?Boolean

Returns:

  • (Boolean)


189
190
191
192
# File 'lib/react_on_rails/react_component/render_options.rb', line 189

def html_streaming?
  # Returns true if the component should be rendered incrementally
  render_mode == :html_streaming
end

#internal_option(key) ⇒ Object



162
163
164
# File 'lib/react_on_rails/react_component/render_options.rb', line 162

def internal_option(key)
  options[key]
end

#logging_on_serverObject



154
155
156
# File 'lib/react_on_rails/react_component/render_options.rb', line 154

def logging_on_server
  retrieve_configuration_value_for(:logging_on_server)
end

#prerenderObject



127
128
129
130
131
132
# File 'lib/react_on_rails/react_component/render_options.rb', line 127

def prerender
  env_override = prerender_env_override
  return env_override unless env_override.nil?

  retrieve_configuration_value_for(:prerender)
end

#propsObject



83
84
85
# File 'lib/react_on_rails/react_component/render_options.rb', line 83

def props
  options.fetch(:props) { NO_PROPS }
end

#raise_non_shell_server_rendering_errorsObject



150
151
152
# File 'lib/react_on_rails/react_component/render_options.rb', line 150

def raise_non_shell_server_rendering_errors
  retrieve_react_on_rails_pro_config_value_for(:raise_non_shell_server_rendering_errors)
end

#raise_on_prerender_errorObject



146
147
148
# File 'lib/react_on_rails/react_component/render_options.rb', line 146

def raise_on_prerender_error
  retrieve_configuration_value_for(:raise_on_prerender_error)
end

#random_dom_idObject



101
102
103
# File 'lib/react_on_rails/react_component/render_options.rb', line 101

def random_dom_id
  retrieve_configuration_value_for(:random_dom_id)
end

#random_dom_id?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
# File 'lib/react_on_rails/react_component/render_options.rb', line 115

def random_dom_id?
  return false if options[:id]

  return false unless random_dom_id

  true
end

#render_modeObject



170
171
172
173
174
175
176
177
# File 'lib/react_on_rails/react_component/render_options.rb', line 170

def render_mode
  # Determines the React rendering strategy:
  # - :sync: Synchronous SSR using renderToString (blocking and rendering in one shot)
  # - :html_streaming: Progressive SSR using renderToPipeableStream (non-blocking and rendering incrementally)
  # - :rsc_payload_streaming: Server Components serialized in React flight format
  #   (non-blocking and rendering incrementally).
  options.fetch(:render_mode, :sync)
end

#replay_consoleObject



142
143
144
# File 'lib/react_on_rails/react_component/render_options.rb', line 142

def replay_console
  retrieve_configuration_value_for(:replay_console)
end

#rsc_payload_streaming?Boolean

Returns:

  • (Boolean)


184
185
186
187
# File 'lib/react_on_rails/react_component/render_options.rb', line 184

def rsc_payload_streaming?
  # Returns true if the component should be rendered as a React Server Component
  render_mode == :rsc_payload_streaming
end

#set_option(key, value) ⇒ Object



166
167
168
# File 'lib/react_on_rails/react_component/render_options.rb', line 166

def set_option(key, value)
  options[key] = value
end

#store_dependenciesObject



194
195
196
# File 'lib/react_on_rails/react_component/render_options.rb', line 194

def store_dependencies
  options[:store_dependencies]
end

#streaming?Boolean

Returns:

  • (Boolean)


179
180
181
182
# File 'lib/react_on_rails/react_component/render_options.rb', line 179

def streaming?
  # Returns true if the component should be rendered incrementally
  %i[html_streaming rsc_payload_streaming].include?(render_mode)
end

#throw_js_errorsObject



79
80
81
# File 'lib/react_on_rails/react_component/render_options.rb', line 79

def throw_js_errors
  options.fetch(:throw_js_errors, false)
end

#to_sObject



158
159
160
# File 'lib/react_on_rails/react_component/render_options.rb', line 158

def to_s
  "{ react_component_name = #{react_component_name}, options = #{options}, request_digest = #{request_digest}"
end

#traceObject



138
139
140
# File 'lib/react_on_rails/react_component/render_options.rb', line 138

def trace
  retrieve_configuration_value_for(:trace)
end