universal_renderer 0.5.2 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,19 +1,119 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UniversalRenderer
4
+ # Scaffolds a working SSR setup: the initializer, the two renderer entry points,
5
+ # the SSR Vite build, the precompile hook, and the Puma plugin that runs the
6
+ # renderer in production. Each one encodes something that is wrong by default
7
+ # and fails silently.
4
8
  class InstallGenerator < Rails::Generators::Base
5
9
  source_root File.expand_path("templates", __dir__)
6
10
 
11
+ PUMA_CONFIG = "config/puma.rb"
12
+ PUMA_PLUGIN_LINE = 'plugin :universal_renderer if ENV.fetch("RAILS_ENV", "development") == "production"'
13
+
14
+ class_option :frontend_dir,
15
+ type: :string,
16
+ default: "app/frontend",
17
+ desc: "Where your JavaScript lives (vite_rails' sourceCodeDir)"
18
+
19
+ class_option :skip_frontend,
20
+ type: :boolean,
21
+ default: false,
22
+ desc: "Only write the Ruby-side files"
23
+
24
+ class_option :skip_deploy,
25
+ type: :boolean,
26
+ default: false,
27
+ desc: "Skip the Puma plugin and the assets:precompile hook"
28
+
7
29
  def copy_initializer
8
30
  template "initializer.rb", "config/initializers/universal_renderer.rb"
9
31
  end
10
32
 
33
+ def copy_frontend
34
+ return if options[:skip_frontend]
35
+
36
+ template "ssr/globals.ts", "#{frontend_dir}/ssr/globals.ts"
37
+ template "ssr/config.ts", "#{frontend_dir}/ssr/config.ts"
38
+ template "ssr/server.ts", "#{frontend_dir}/ssr/server.ts"
39
+ template "ssr/dev.ts", "#{frontend_dir}/ssr/dev.ts"
40
+ template "vite.config.ssr.mts", "vite.config.ssr.mts"
41
+ end
42
+
43
+ def copy_deploy_files
44
+ return if options[:skip_deploy] || options[:skip_frontend]
45
+
46
+ template "ssr.rake", "lib/tasks/ssr.rake"
47
+ add_puma_plugin if puma_setup == :add
48
+ end
49
+
11
50
  def show_installation_notes
12
- say_status "info", "Universal Renderer installed successfully!"
51
+ say_status "info", "Universal Renderer installed."
13
52
  say_status "note", "Next steps:"
53
+ if options[:skip_frontend]
54
+ say_status "", " 1. Review config/initializers/universal_renderer.rb"
55
+ say_status "", " 2. Connect your existing renderer to that endpoint"
56
+ say_status "", " 3. Opt a controller in with `enable_ssr` or `render_ssr`"
57
+ return
58
+ end
59
+
60
+ say_status "", " 1. bun add universal-renderer (or npm/yarn)"
61
+ say_status "", " 2. Fill in #{frontend_dir}/ssr/config.ts — the render itself"
62
+ say_status "", " 3. Add the renderer to Procfile.dev:"
63
+ say_status "", " ssr: bun #{frontend_dir}/ssr/dev.ts"
64
+ say_status "", " 4. Add a build script to package.json:"
14
65
  say_status "",
15
- " 1. Edit config/initializers/universal_renderer.rb to point at your SSR server"
16
- say_status "", " 2. Ensure your Node.js/Bun SSR server is running"
66
+ ' "build:ssr": "vite -c vite.config.ssr.mts build"'
67
+ say_status "", " 5. Opt a controller in with `enable_ssr` or `render_ssr`"
68
+ return if options[:skip_deploy]
69
+
70
+ case puma_setup
71
+ when :add
72
+ say_status "", " 6. Puma now starts the renderer in production (`plugin"
73
+ say_status "", " :universal_renderer` in config/puma.rb). Keep Procfile's web"
74
+ say_status "", " process as it is"
75
+ when :bin_web
76
+ say_status "", " 6. bin/web is no longer generated. Point Procfile's web process"
77
+ say_status "", " at Puma, delete bin/web, and add to config/puma.rb:"
78
+ say_status "", " #{PUMA_PLUGIN_LINE}"
79
+ when :no_config
80
+ say_status "", " 6. Start the renderer with your app server. With Puma, add to"
81
+ say_status "", " its config:"
82
+ say_status "", " #{PUMA_PLUGIN_LINE}"
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def frontend_dir
89
+ options[:frontend_dir].delete_suffix("/")
90
+ end
91
+
92
+ # Decided once, before any file is written. An app that still runs the
93
+ # bin/web from an earlier version is left alone: adding the plugin as well
94
+ # would start a second renderer on the same port.
95
+ def puma_setup
96
+ @puma_setup ||=
97
+ if File.exist?(File.join(destination_root, "bin/web"))
98
+ :bin_web
99
+ elsif File.exist?(File.join(destination_root, PUMA_CONFIG))
100
+ :add
101
+ else
102
+ :no_config
103
+ end
104
+ end
105
+
106
+ def add_puma_plugin
107
+ return if File.read(File.join(destination_root, PUMA_CONFIG)).match?(/^\s*plugin :universal_renderer\b/)
108
+
109
+ append_to_file PUMA_CONFIG, <<~RUBY
110
+
111
+ # Runs the SSR renderer beside Puma, and restarts it when it exits or
112
+ # stops answering /health. Production only: in development, Procfile.dev
113
+ # runs the development renderer on the same port.
114
+ # Options: `universal_renderer runtime: "bun", bundle: "ssr-build/server.mjs", port: 3001`.
115
+ #{PUMA_PLUGIN_LINE}
116
+ RUBY
17
117
  end
18
118
  end
19
119
  end
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  UniversalRenderer.configure do |c|
4
- # External Node.js/Bun SSR server. Supports streaming via the /stream endpoint.
5
- c.url = "http://localhost:3001"
4
+ c.url = ENV.fetch("UNIVERSAL_RENDERER_URL", "http://localhost:3001")
5
+
6
+ # Keep this above the renderer's 2.5-second default.
6
7
  c.timeout = 3
7
- c.stream_path = "/stream"
8
+
8
9
  c.http.pool_size = 5
9
10
 
10
- # Blocking SSR is the default. Enable streaming per controller only when needed:
11
- # enable_ssr streaming: true
11
+ # c.sanitize = false
12
+ # c.scrubber = MyScrubber.new
13
+ # c.auto_include = false
14
+ # c.on_error = ->(error, context) { Sentry.capture_exception(error, extra: context) }
12
15
  end
@@ -0,0 +1,40 @@
1
+ // The render lifecycle: setup may await; prepare mutates shared state immediately
2
+ // before rendering; cleanup restores it. With concurrency: 1, prepare through
3
+ // cleanup never overlap.
4
+
5
+ import { renderToString } from "react-dom/server";
6
+
7
+ import type { SsrConfig } from "universal-renderer";
8
+
9
+ import { setBrowserLocation } from "./globals";
10
+
11
+ export default {
12
+ setup: async (url, props) => {
13
+ const { pathname, search } = new URL(url);
14
+ setBrowserLocation(url);
15
+
16
+ // hydrateReactQuery(props, queryClient);
17
+ // await preloadRoute(pathname);
18
+
19
+ const location = `${pathname}${search}`;
20
+ // const app = <StaticRouter location={location}><App /></StaticRouter>;
21
+
22
+ return { location, props, app: null as any };
23
+ },
24
+
25
+ prepare: (context) => {
26
+ // context.previousFlags = { ...FEATURE_FLAGS };
27
+ // Object.assign(FEATURE_FLAGS, context.props.feature_flags);
28
+ },
29
+
30
+ render: (context) => ({
31
+ body: renderToString(context.app),
32
+ // head: context.sheet.getStyleTags(),
33
+ // payload: { queryCache: dehydrate(queryClient) },
34
+ }),
35
+
36
+ cleanup: (context) => {
37
+ // Object.assign(FEATURE_FLAGS, context.previousFlags);
38
+ // context.sheet?.seal();
39
+ },
40
+ } satisfies SsrConfig<any>;
@@ -0,0 +1,9 @@
1
+ // Development renderer entry. Loads the render config through a middleware-mode
2
+ // Vite server, so plugins and path aliases apply and edits need no rebuild.
3
+ //
4
+ // Production does not go through this file. See server.ts.
5
+ import "./globals";
6
+
7
+ const { startDevServer } = await import("universal-renderer/dev");
8
+
9
+ await startDevServer({ entry: "<%= frontend_dir %>/ssr/config.ts" });
@@ -0,0 +1,141 @@
1
+ // App-owned browser stubs for SSR. Add only what your graph needs. Defining
2
+ // `window` makes browser-detection checks false process-wide; prefer fixing DOM
3
+ // access in the app where possible.
4
+
5
+ const noop = () => undefined;
6
+ const g = globalThis as any;
7
+ const MARKER = "__ssrBrowserGlobals";
8
+
9
+ export const isShimmed = (): boolean => g[MARKER] === true;
10
+ export const SSR_VIEWPORT = { width: 1024, height: 768 };
11
+
12
+ const makeLocation = (value: string) => {
13
+ const url = new URL(value);
14
+ return {
15
+ href: url.href,
16
+ protocol: url.protocol,
17
+ host: url.host,
18
+ hostname: url.hostname,
19
+ port: url.port,
20
+ pathname: url.pathname,
21
+ search: url.search,
22
+ hash: url.hash,
23
+ origin: url.origin,
24
+ assign: noop,
25
+ reload: noop,
26
+ replace: noop,
27
+ toString: () => url.href,
28
+ };
29
+ };
30
+
31
+ const makeStorage = (): Storage =>
32
+ ({
33
+ length: 0,
34
+ clear: noop,
35
+ getItem: () => null,
36
+ key: () => null,
37
+ removeItem: noop,
38
+ setItem: noop,
39
+ }) as unknown as Storage;
40
+
41
+ const makeElement = (): any => ({
42
+ style: {},
43
+ dataset: {},
44
+ children: [],
45
+ classList: { add: noop, remove: noop, toggle: noop, contains: () => false },
46
+ setAttribute: noop,
47
+ removeAttribute: noop,
48
+ getAttribute: () => null,
49
+ appendChild: noop,
50
+ removeChild: noop,
51
+ addEventListener: noop,
52
+ removeEventListener: noop,
53
+ querySelector: () => null,
54
+ querySelectorAll: () => [],
55
+ contains: () => false,
56
+ });
57
+
58
+ export function setBrowserLocation(url: string): void {
59
+ if (!isShimmed()) return;
60
+
61
+ const location = makeLocation(url);
62
+ g.window.location = location;
63
+ g.location = location;
64
+ if (g.document) g.document.URL = location.href;
65
+ }
66
+
67
+ export function installBrowserGlobals(): boolean {
68
+ if (isShimmed()) return false;
69
+ if (typeof g.window !== "undefined" && typeof g.document !== "undefined") {
70
+ return false;
71
+ }
72
+
73
+ g[MARKER] = true;
74
+ const location = makeLocation("http://localhost:3000/");
75
+ const win: any = {
76
+ location,
77
+ localStorage: makeStorage(),
78
+ sessionStorage: makeStorage(),
79
+ navigator: { userAgent: "ssr", language: "en-US", languages: ["en-US"] },
80
+ document: {
81
+ readyState: "complete",
82
+ title: "",
83
+ cookie: "",
84
+ URL: location.href,
85
+ documentElement: makeElement(),
86
+ body: makeElement(),
87
+ head: makeElement(),
88
+ hidden: false,
89
+ visibilityState: "visible",
90
+ createElement: () => makeElement(),
91
+ getElementById: () => null,
92
+ querySelector: () => null,
93
+ querySelectorAll: () => [],
94
+ addEventListener: noop,
95
+ removeEventListener: noop,
96
+ },
97
+ history: { pushState: noop, replaceState: noop, back: noop, go: noop },
98
+ matchMedia: (media: string) => ({
99
+ matches: false,
100
+ media,
101
+ addListener: noop,
102
+ removeListener: noop,
103
+ addEventListener: noop,
104
+ removeEventListener: noop,
105
+ }),
106
+ getComputedStyle: () => ({ getPropertyValue: () => "" }),
107
+ requestAnimationFrame: (_cb: () => void) => 0,
108
+ cancelAnimationFrame: noop,
109
+ innerWidth: SSR_VIEWPORT.width,
110
+ innerHeight: SSR_VIEWPORT.height,
111
+ scrollTo: noop,
112
+ addEventListener: noop,
113
+ removeEventListener: noop,
114
+ };
115
+
116
+ win.window = win;
117
+ win.self = win;
118
+ win.top = win;
119
+
120
+ g.window = win;
121
+ for (const key of [
122
+ "document",
123
+ "navigator",
124
+ "location",
125
+ "history",
126
+ "localStorage",
127
+ "sessionStorage",
128
+ "matchMedia",
129
+ "getComputedStyle",
130
+ "requestAnimationFrame",
131
+ "cancelAnimationFrame",
132
+ "innerWidth",
133
+ "innerHeight",
134
+ ]) {
135
+ if (!(key in g)) g[key] = win[key];
136
+ }
137
+
138
+ return true;
139
+ }
140
+
141
+ installBrowserGlobals();
@@ -0,0 +1,24 @@
1
+ // Production renderer entry. Renders from the bundle vite.config.ssr.mts builds,
2
+ // with no Vite server and no per-request module transform. Development uses
3
+ // dev.ts instead.
4
+ //
5
+ // Browser globals first. See globals.ts, and delete it if your graph needs none.
6
+ import "./globals";
7
+
8
+ // Dynamic, not static: static imports all evaluate before the entry body runs, so
9
+ // the import above would land after the app graph has already touched globals.
10
+ //
11
+ // A library that snapshots `typeof window` at module scope must be imported
12
+ // statically *above* `./globals`. Leave a comment saying so, or a formatter
13
+ // re-sorting the imports undoes it.
14
+ const { default: config } = await import("./config");
15
+ const { startServer } = await import("universal-renderer");
16
+
17
+ await startServer({
18
+ ...config,
19
+ // Serialized on purpose. See config.ts.
20
+ concurrency: 1,
21
+ // Keep this below the gem's three-second default timeout: a running render
22
+ // holds its slot, so the renderer must give up before Rails falls back.
23
+ renderTimeout: 2_500,
24
+ });
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # No :environment dependency: bundling the frontend does not need Rails booted,
4
+ # and booting it during precompile only adds ways for the build to fail —
5
+ # credentials, a database that is not up yet.
6
+ # rubocop:disable Rails/RakeEnvironment
7
+ namespace :ssr do
8
+ desc "Build the standalone SSR bundle that the Puma plugin runs (ssr-build/)"
9
+ task :build do
10
+ # Goes through the package script rather than invoking `vite` directly:
11
+ # node_modules/.bin is not on PATH during a deploy build, so a bare `vite`
12
+ # fails there while working fine in a shell with the toolchain loaded.
13
+ # Swap `bun run` for `npm run` / `yarn` to match your package manager.
14
+ sh "bun run build:ssr"
15
+ end
16
+ end
17
+ # rubocop:enable Rails/RakeEnvironment
18
+
19
+ # The Puma plugin runs the compiled bundle, so it has to exist by the time a release
20
+ # boots. Build it with the rest of the assets.
21
+ if Rake::Task.task_defined?("assets:precompile")
22
+ Rake::Task["assets:precompile"].enhance(["ssr:build"])
23
+ else
24
+ # No asset pipeline here, so define the task rather than skipping the build
25
+ # and shipping a release with no renderer.
26
+ Rake::Task.define_task("assets:precompile" => "ssr:build")
27
+ end
@@ -0,0 +1,15 @@
1
+ import react from "@vitejs/plugin-react";
2
+ import { defineSsrConfig } from "universal-renderer/vite";
3
+
4
+ // Standalone SSR build. defineSsrConfig handles the six settings that are wrong
5
+ // by default for a Rails SSR build; read its docs before overriding them.
6
+ //
7
+ // Note what is absent: vite-plugin-rails, which targets the client manifest
8
+ // pipeline and overrides entrypoints and outDir. Your client build keeps using
9
+ // vite.config.mts, unchanged.
10
+ export default defineSsrConfig({
11
+ entry: "<%= frontend_dir %>/ssr/server.ts",
12
+ plugins: [react()],
13
+ // Dependencies are bundled. List client-only packages that cannot be (native
14
+ // modules) here: ssr: { external: ["react-pdf"] },
15
+ });
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "puma/dsl"
4
+ require "puma/plugin"
5
+ require_relative "../../universal_renderer/supervisor"
6
+
7
+ module UniversalRenderer
8
+ # Starts and supervises the SSR renderer from Puma.
9
+ #
10
+ # # config/puma.rb
11
+ # plugin :universal_renderer
12
+ # universal_renderer port: 3001 # optional, after `plugin`
13
+ #
14
+ # Puma runs plugins in its master process, so a clustered server gets one
15
+ # renderer, not one per worker.
16
+ module PumaPlugin
17
+ LOG_PREFIX = Supervisor::LOG_PREFIX
18
+
19
+ # Each option falls back to an environment variable, then to the default.
20
+ OPTION_DEFAULTS = {
21
+ runtime: ["SSR_RUNTIME", "bun"],
22
+ bundle: ["SSR_BUNDLE", "ssr-build/server.mjs"],
23
+ port: ["SSR_PORT", "3001"]
24
+ }.freeze
25
+
26
+ # Puma DSL methods. `plugin :universal_renderer` loads this file, so call
27
+ # `universal_renderer` after that line.
28
+ module DSL
29
+ # @option options [String] :runtime (`SSR_RUNTIME`, `"bun"`)
30
+ # @option options [String] :bundle (`SSR_BUNDLE`, `"ssr-build/server.mjs"`)
31
+ # When it does not exist, the renderer is not started and pages render
32
+ # on the client.
33
+ # @option options [Integer] :port (`SSR_PORT`, 3001)
34
+ def universal_renderer(**options)
35
+ @options[:universal_renderer] = options
36
+ end
37
+ end
38
+
39
+ def self.resolve(options, env: ENV)
40
+ unknown = options.keys - OPTION_DEFAULTS.keys
41
+ raise ArgumentError, "unknown universal_renderer option(s): #{unknown.join(', ')}" if unknown.any?
42
+
43
+ resolved =
44
+ OPTION_DEFAULTS.to_h do |key, (variable, default)|
45
+ [key, options.fetch(key) { env.fetch(variable, default) }]
46
+ end
47
+ resolved[:port] = Integer(resolved[:port])
48
+ resolved
49
+ end
50
+
51
+ def self.start(launcher)
52
+ log_writer = launcher.log_writer
53
+ log = ->(message) { log_writer.log(message) }
54
+ options = resolve(launcher.options[:universal_renderer] || {})
55
+
56
+ unless File.exist?(options[:bundle])
57
+ return log.call("#{LOG_PREFIX} #{options[:bundle]} missing, serving client-rendered pages only")
58
+ end
59
+
60
+ command = [options[:runtime], options[:bundle]]
61
+ supervise(launcher, Supervisor.new(command: command, port: options[:port], logger: log))
62
+ end
63
+
64
+ def self.supervise(launcher, supervisor)
65
+ supervisor.start
66
+ on(launcher.events, :after_stopped, :on_stopped) { supervisor.stop }
67
+
68
+ # Fires for hot and phased restarts alike, and a plugin cannot tell which.
69
+ # A hot restart execs right after, so the supervisor's next renderer (one
70
+ # second later) never starts and the new master boots its own. A phased
71
+ # restart keeps this master, so the supervisor starts a renderer with the
72
+ # new bundle. Either way the old one must release the port first.
73
+ on(launcher.events, :before_restart, :on_restart) { supervisor.restart(reason: "Puma restart") }
74
+
75
+ # `after_stopped` does not run when the master dies some other way, and the
76
+ # renderer is in its own process group, so nothing else would stop it.
77
+ # Workers inherit this hook when they fork, hence the pid check.
78
+ master_pid = Process.pid
79
+ at_exit { supervisor.stop if Process.pid == master_pid && !supervisor.stopping? }
80
+ supervisor
81
+ end
82
+
83
+ # Puma 7 renamed its lifecycle hooks (`on_stopped` became `after_stopped`).
84
+ # Old Pumas without either still stop the renderer through `at_exit`.
85
+ def self.on(events, *names, &)
86
+ name = names.find { |candidate| events.respond_to?(candidate) }
87
+ events.public_send(name, &) if name
88
+ end
89
+ end
90
+ end
91
+
92
+ Puma::DSL.include(UniversalRenderer::PumaPlugin::DSL)
93
+
94
+ Puma::Plugin.create do
95
+ def start(launcher) # rubocop:disable Rails/Delegate -- the Puma master may not have loaded ActiveSupport
96
+ UniversalRenderer::PumaPlugin.start(launcher)
97
+ end
98
+ end
@@ -16,6 +16,9 @@ module UniversalRenderer
16
16
  # This is used for non-streaming SSR, where the entire payload is fetched
17
17
  # before the main application view is rendered.
18
18
  #
19
+ # Emits a `render.universal_renderer` notification for every attempt and
20
+ # routes failures through `config.on_error`.
21
+ #
19
22
  # @param url [String] The URL of the page to render on the SSR server.
20
23
  # This should typically be the `request.original_url` from the controller.
21
24
  # @param props [Hash] A hash of props to be passed to the SSR service.
@@ -25,54 +28,106 @@ module UniversalRenderer
25
28
  # (HTTP 2xx). Returns `nil` when the request fails or the SSR service is
26
29
  # unreachable.
27
30
  def self.call(url, props)
28
- ssr_url = UniversalRenderer.config.url
29
- return if ssr_url.blank?
30
-
31
- timeout = UniversalRenderer.config.timeout
32
-
33
- begin
34
- uri = URI.parse(ssr_url)
35
- request = Net::HTTP::Post.new(uri.request_uri)
36
- request.body = { url: url, props: props }.to_json
37
- request["Content-Type"] = "application/json"
38
-
39
- response = HttpPool.request(uri, timeout, request)
40
-
41
- if response.is_a?(Net::HTTPSuccess)
42
- raw_data = JSON.parse(response.body).deep_symbolize_keys
43
-
44
- # Map the keys we care about to the Struct. The Node service might
45
- # send `:body_html` instead of `:body`; favour the latter if
46
- # present but fall back gracefully.
47
- UniversalRenderer::SSR::Response.new(
48
- head: raw_data[:head],
49
- body: raw_data[:body] || raw_data[:body_html],
50
- body_attrs: raw_data[:body_attrs]
51
- )
52
- else
53
- UniversalRenderer.log do |log|
54
- log.error(
55
- "SSR fetch request to #{ssr_url} failed: #{response.code} - #{response.message} (URL: #{url})"
56
- )
57
- end
58
- nil
59
- end
60
- rescue Net::OpenTimeout, Net::ReadTimeout => e
61
- UniversalRenderer.log do |log|
62
- log.error(
63
- "SSR fetch request to #{ssr_url} timed out: #{e.class.name} - #{e.message} (URL: #{url})"
64
- )
31
+ config = UniversalRenderer.config
32
+ ssr_url = config.url
33
+
34
+ Instrumentation.instrument(url: url, mode: :blocking) do |event|
35
+ if ssr_url.blank?
36
+ event[:outcome] = :not_configured
37
+ next nil
65
38
  end
66
- nil
67
- rescue StandardError => e
68
- UniversalRenderer.log do |log|
69
- log.error(
70
- "SSR fetch request to #{ssr_url} failed: #{e.class.name} - #{e.message} (URL: #{url})"
71
- )
39
+
40
+ perform(ssr_url, config, url, props, event)
41
+ end
42
+ end
43
+
44
+ def self.perform(ssr_url, config, url, props, event)
45
+ # A nil render_path means the path in `url` is already the endpoint.
46
+ parsed = URI.parse(ssr_url)
47
+ uri =
48
+ if config.render_path.present?
49
+ URI.join(parsed, absolute_path(config.render_path))
50
+ else
51
+ parsed
72
52
  end
73
- nil
53
+
54
+ request = Net::HTTP::Post.new(uri.request_uri)
55
+ request.body = { url: url, props: props }.to_json
56
+ request["Content-Type"] = "application/json"
57
+
58
+ response = HttpPool.request(uri, config.timeout, request)
59
+
60
+ unless response.is_a?(Net::HTTPSuccess)
61
+ event[:outcome] = :http_error
62
+ event[:status] = response.code.to_i
63
+ fail_render(
64
+ url,
65
+ uri,
66
+ event,
67
+ "responded with #{response.code} #{response.message}"
68
+ )
69
+ return nil
70
+ end
71
+
72
+ build_response(JSON.parse(response.body))
73
+ rescue Net::OpenTimeout, Net::ReadTimeout => e
74
+ event[:outcome] = :timeout
75
+ event[:error] = e
76
+ fail_render(url, uri, event, "timed out: #{e.class.name} - #{e.message}", e)
77
+ nil
78
+ rescue StandardError => e
79
+ event[:outcome] = :error
80
+ event[:error] = e
81
+ fail_render(url, uri, event, "failed: #{e.class.name} - #{e.message}", e)
82
+ nil
83
+ end
84
+
85
+ # String keys on purpose: `payload` can be a large dehydrated query cache,
86
+ # and deep-symbolizing it would walk and re-allocate all of it per render.
87
+ #
88
+ # The three keys that reach a view helper are type-checked, so a renderer
89
+ # answering 200 with the wrong shape falls back like any other failed
90
+ # render instead of raising mid-layout. `perform` rescues the TypeError.
91
+ def self.build_response(data)
92
+ unless data.is_a?(Hash)
93
+ raise TypeError,
94
+ "SSR service returned #{data.class.name}, expected a JSON object"
95
+ end
96
+
97
+ UniversalRenderer::SSR::Response.new(
98
+ head: string_or_nil(data["head"]),
99
+ body: string_or_nil(data["body"]),
100
+ body_attrs: (data["body_attrs"] if data["body_attrs"].is_a?(Hash)),
101
+ payload: data["payload"]
102
+ )
103
+ end
104
+
105
+ def self.string_or_nil(value)
106
+ value if value.is_a?(String)
107
+ end
108
+
109
+ # URI.join replaces the base URL's last path segment when the joined path is
110
+ # relative, which would post renders to the wrong endpoint.
111
+ def self.absolute_path(path)
112
+ path = path.to_s
113
+ path.start_with?("/") ? path : "/#{path}"
114
+ end
115
+
116
+ def self.fail_render(url, uri, event, message, error = nil)
117
+ target = uri ? uri.to_s : UniversalRenderer.config.url.to_s
118
+
119
+ UniversalRenderer.log do |log|
120
+ log.error("SSR fetch request to #{target} #{message} (URL: #{url})")
74
121
  end
122
+
123
+ Instrumentation.report(
124
+ error || StandardError.new("SSR fetch request #{message}"),
125
+ event.merge(target: target)
126
+ )
75
127
  end
128
+
129
+ private_class_method :perform, :build_response, :string_or_nil,
130
+ :fail_render, :absolute_path
76
131
  end
77
132
  end
78
133
  end