Class: Inertia::SSR::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia/ssr/client.rb

Overview

HTTP client for communicating with the SSR server.

Sends page data to the SSR server and receives rendered HTML containing the head and body fragments.

Class Method Summary collapse

Class Method Details

.render(data) ⇒ Hash

Renders the Inertia page data on the SSR server.

Parameters:

  • data (Hash)

    the Inertia page object to render

Returns:

  • (Hash)

    parsed response containing "head" and "body" keys



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/inertia/ssr/client.rb', line 19

def render(data)
  response = begin
    Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https", open_timeout: 1, read_timeout: 1) do |http|
      http.post(uri.request_uri, data.to_json, "Content-Type" => "application/json")
    end
  rescue => e
    raise Inertia::SSRServerError, e
  end

  unless response.code == "200"
    raise Inertia::SSRServerError, "Request failed with status #{response.code}: #{response.body}"
  end

  JSON.parse(response.body)
end