Module: UniversalRenderer::SSR::Helpers

Defined in:
lib/universal_renderer/ssr/helpers.rb

Overview

View helpers for emitting what the SSR service returned. ssr?, ssr_response, and ssr_streaming? come from the controller via helper_method (see Renderable).

Constant Summary collapse

SAFE_BODY_ATTRIBUTE_NAME =
/\A[a-z_:][a-z0-9:._-]*\z/i

Instance Method Summary collapse

Instance Method Details

#ssr_bodyString

Server-rendered body content, or the streaming placeholder.

Returns:

  • (String)

    Sanitized body HTML, the <!-- SSR_BODY --> marker when streaming, or an empty string when there is nothing to emit.



24
25
26
27
28
29
# File 'lib/universal_renderer/ssr/helpers.rb', line 24

def ssr_body
  return Placeholders::BODY if ssr_streaming?

  html = ssr_response&.body
  html.present? ? sanitize_ssr(html) : ""
end

#ssr_body_attributesActiveSupport::SafeBuffer

Attributes the renderer asked to be applied to the <body> tag:

>

A non-Hash is dropped rather than raised on, so a renderer answering 200 with the wrong shape degrades the page instead of breaking the layout.

Returns:

  • (ActiveSupport::SafeBuffer)

    Escaped name="value" pairs, or an empty buffer when the renderer sent none.



40
41
42
43
44
45
46
47
# File 'lib/universal_renderer/ssr/helpers.rb', line 40

def ssr_body_attributes
  attrs = ssr_response&.body_attrs
  return "".html_safe unless attrs.is_a?(Hash)
  return "".html_safe if attrs.empty?

  attrs = sanitize_ssr_body_attributes(attrs) if UniversalRenderer.config.sanitize
  tag.attributes(attrs)
end

#ssr_headString

Server-rendered content, or the streaming placeholder.

Returns:

  • (String)

    Sanitized head HTML, the <!-- SSR_HEAD --> marker when streaming, or an empty string when there is nothing to emit.



13
14
15
16
17
18
# File 'lib/universal_renderer/ssr/helpers.rb', line 13

def ssr_head
  return Placeholders::HEAD if ssr_streaming?

  html = ssr_response&.head
  html.present? ? sanitize_ssr(html) : ""
end

#ssr_payload(id: "ssr-payload") ⇒ ActiveSupport::SafeBuffer?

Renders the renderer's hydration payload as an inert JSON script tag.

Return it as payload from your render callback and read it on the client with JSON.parse(el.textContent).

Parameters:

  • id (String) (defaults to: "ssr-payload")

    DOM id for the script element.

Returns:

  • (ActiveSupport::SafeBuffer, nil)

    The script tag, or nil when the renderer sent no payload.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/universal_renderer/ssr/helpers.rb', line 57

def ssr_payload(id: "ssr-payload")
  payload = ssr_response&.payload
  return if payload.nil?

  (
    :script,
    ERB::Util.json_escape(payload.to_json),
    { id: id, type: "application/json" },
    false
  )
end