Module: ReactOnRailsPro::RSCPayloadRenderer

Extended by:
ActiveSupport::Concern
Included in:
RscPayloadController
Defined in:
lib/react_on_rails_pro/concerns/rsc_payload_renderer.rb

Instance Method Summary collapse

Instance Method Details

#rsc_payloadObject



12
13
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
# File 'lib/react_on_rails_pro/concerns/rsc_payload_renderer.rb', line 12

def rsc_payload
  @rsc_payload_component_name = rsc_payload_component_name
  @rsc_payload_component_props =
    begin
      rsc_payload_component_props
    rescue JSON::ParserError => e
      Rails.logger.warn(
        "[React on Rails Pro] Invalid JSON passed to the RSC payload endpoint " \
        "for component '#{@rsc_payload_component_name}': #{e.message}"
      )
      return render plain: "Invalid props JSON", status: :bad_request
    end

  stream_view_containing_react_components(
    template: custom_rsc_payload_template,
    layout: false,
    # Render as text so Rails does not inject HTML view annotation comments
    # into the NDJSON stream. Custom template overrides must resolve to a
    # text or format-neutral template, not `.html.erb`.
    formats: [:text],
    content_type: "application/x-ndjson"
  )
rescue ActionView::MissingTemplate => e
  raise e.exception(
    "[React on Rails Pro] RSC payload templates are now rendered with format :text. " \
    "If you override `custom_rsc_payload_template`, make sure the override resolves to " \
    "a text or format-neutral template (for example `rsc_payload.text.erb`) instead of " \
    "only `.html.erb`. See " \
    "https://github.com/shakacode/react_on_rails/blob/master/docs/pro/updating.md " \
    "for upgrade notes.\n\n" \
    "Original error: #{e.message}"
  )
end