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
-
#ssr_body ⇒ String
Server-rendered body content, or the streaming placeholder.
-
#ssr_body_attributes ⇒ ActiveSupport::SafeBuffer
Attributes the renderer asked to be applied to the
<body>tag:. -
#ssr_head ⇒ String
Server-rendered
content, or the streaming placeholder. -
#ssr_payload(id: "ssr-payload") ⇒ ActiveSupport::SafeBuffer?
Renders the renderer's hydration payload as an inert JSON script tag.
Instance Method Details
#ssr_body ⇒ String
Server-rendered body content, or the streaming placeholder.
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_attributes ⇒ ActiveSupport::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.
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_head ⇒ String
Server-rendered
content, or the streaming placeholder.
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).
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? content_tag( :script, ERB::Util.json_escape(payload.to_json), { id: id, type: "application/json" }, false ) end |