Class: UniversalRenderer::Client::Stream

Inherits:
Object
  • Object
show all
Extended by:
ErrorLogger, Execution, Setup
Defined in:
lib/universal_renderer/client/stream.rb,
lib/universal_renderer/client/stream/setup.rb,
lib/universal_renderer/client/stream/execution.rb,
lib/universal_renderer/client/stream/error_logger.rb

Defined Under Namespace

Modules: ErrorLogger, Execution, Setup

Class Method Summary collapse

Methods included from ErrorLogger

log_connection_error, log_setup_error, log_unexpected_error

Methods included from Execution

perform_streaming

Methods included from Setup

build_stream_request_components, ensure_ssr_server_url_configured?

Class Method Details

.call(url, props, template, response) ⇒ Boolean

Orchestrates the streaming process for server-side rendering.

Parameters:

  • url (String)

    The URL of the page to render.

  • props (Hash)

    Data to be passed for rendering, including layout HTML.

  • template (String)

    The HTML template to use for rendering.

  • response (ActionDispatch::Response)

    The Rails response object to stream to.

Returns:

  • (Boolean)

    True if streaming was initiated, false otherwise.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/universal_renderer/client/stream.rb', line 21

def self.call(url, props, template, response)
  config = UniversalRenderer.config

  unless Setup.ensure_ssr_server_url_configured?(config)
    UniversalRenderer.log do |log|
      log.warn(
        "Stream: SSR URL (config.url) is not configured. Falling back."
      )
    end
    return false
  end

  stream_uri_obj = nil
  full_ssr_url_for_log = config.url.to_s # For logging in case of early error

  begin
    body = { url: url, props: props, template: template }

    actual_stream_uri, http_client, http_post_request =
      Setup.build_stream_request_components(body, config)

    stream_uri_obj = actual_stream_uri

    full_ssr_url_for_log = actual_stream_uri.to_s # Update for more specific logging
  rescue URI::InvalidURIError => e
    UniversalRenderer.log do |log|
      log.error(
        "Stream: SSR stream failed due to invalid URI ('#{config.url}'): #{e.message}"
      )
    end
    return false
  rescue StandardError => e
    log_setup_error(e, full_ssr_url_for_log)
    return false
  end

  Execution.perform_streaming(
    http_client,
    http_post_request,
    response,
    stream_uri_obj
  )
rescue Errno::ECONNREFUSED,
       Errno::EHOSTUNREACH,
       Net::OpenTimeout,
       Net::ReadTimeout,
       SocketError,
       IOError => e
  uri_str_for_conn_error =
    stream_uri_obj ? stream_uri_obj.to_s : full_ssr_url_for_log

  ErrorLogger.log_connection_error(e, uri_str_for_conn_error)

  false
rescue StandardError => e
  uri_str_for_unexpected_error =
    stream_uri_obj ? stream_uri_obj.to_s : full_ssr_url_for_log

  ErrorLogger.log_unexpected_error(
    e,
    uri_str_for_unexpected_error,
    "Stream: Unexpected error during SSR stream process"
  )

  false
end