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



73
74
75
76
77
# File 'lib/react_on_rails/react_component/render_options.rb', line 73

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.



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

def hydrate_on
  @hydrate_on
end

#react_component_nameObject (readonly)

Returns the value of attribute react_component_name.



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

def react_component_name
  @react_component_name
end

#request_digestObject

Returns the value of attribute request_digest.



29
30
31
# File 'lib/react_on_rails/react_component/render_options.rb', line 29

def request_digest
  @request_digest
end

Class Method Details

.generate_request_idObject



200
201
202
# File 'lib/react_on_rails/react_component/render_options.rb', line 200

def self.generate_request_id
  SecureRandom.uuid
end

.prerender_env_overrideObject



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

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



47
48
49
50
51
# File 'lib/react_on_rails/react_component/render_options.rb', line 47

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



136
137
138
# File 'lib/react_on_rails/react_component/render_options.rb', line 136

def auto_load_bundle
  retrieve_configuration_value_for(:auto_load_bundle)
end

#client_propsObject



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

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



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

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



125
126
127
# File 'lib/react_on_rails/react_component/render_options.rb', line 125

def html_options
  options[:html_options].to_h
end

#html_streaming?Boolean

Returns:

  • (Boolean)


191
192
193
194
# File 'lib/react_on_rails/react_component/render_options.rb', line 191

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

#internal_option(key) ⇒ Object



164
165
166
# File 'lib/react_on_rails/react_component/render_options.rb', line 164

def internal_option(key)
  options[key]
end

#logging_on_serverObject



156
157
158
# File 'lib/react_on_rails/react_component/render_options.rb', line 156

def logging_on_server
  retrieve_configuration_value_for(:logging_on_server)
end

#prerenderObject



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

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

  retrieve_configuration_value_for(:prerender)
end

#propsObject



85
86
87
# File 'lib/react_on_rails/react_component/render_options.rb', line 85

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

#raise_non_shell_server_rendering_errorsObject



152
153
154
# File 'lib/react_on_rails/react_component/render_options.rb', line 152

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



148
149
150
# File 'lib/react_on_rails/react_component/render_options.rb', line 148

def raise_on_prerender_error
  retrieve_configuration_value_for(:raise_on_prerender_error)
end

#random_dom_idObject



103
104
105
# File 'lib/react_on_rails/react_component/render_options.rb', line 103

def random_dom_id
  retrieve_configuration_value_for(:random_dom_id)
end

#random_dom_id?Boolean

Returns:

  • (Boolean)


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

def random_dom_id?
  return false if options[:id]

  return false unless random_dom_id

  true
end

#render_modeObject



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

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



144
145
146
# File 'lib/react_on_rails/react_component/render_options.rb', line 144

def replay_console
  retrieve_configuration_value_for(:replay_console)
end

#rsc_payload_streaming?Boolean

Returns:

  • (Boolean)


186
187
188
189
# File 'lib/react_on_rails/react_component/render_options.rb', line 186

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



168
169
170
# File 'lib/react_on_rails/react_component/render_options.rb', line 168

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

#store_dependenciesObject



196
197
198
# File 'lib/react_on_rails/react_component/render_options.rb', line 196

def store_dependencies
  options[:store_dependencies]
end

#streaming?Boolean

Returns:

  • (Boolean)


181
182
183
184
# File 'lib/react_on_rails/react_component/render_options.rb', line 181

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

#throw_js_errorsObject



81
82
83
# File 'lib/react_on_rails/react_component/render_options.rb', line 81

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

#to_sObject



160
161
162
# File 'lib/react_on_rails/react_component/render_options.rb', line 160

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

#traceObject



140
141
142
# File 'lib/react_on_rails/react_component/render_options.rb', line 140

def trace
  retrieve_configuration_value_for(:trace)
end