re-frame.ssr.ring.node¶
Render the page body on a Node sidecar instead of the JVM, for a view layer whose components are JavaScript, such as a Fresco root. renderer returns a value for ssr-handler's :renderer: for each request it sends the settled request frame's state to the Node render sidecar and takes back the body markup. The head, payload, shell, status, headers and error projection stay on the JVM. Render on Node walks through the bundle, the sidecar and deployment.
Ships in the day8/re-frame2-ssr-ring artefact, beside re-frame.ssr.ring. That jar also carries the Node render sidecar, which renderer launches and supervises itself when given :module; Run the sidecar, and mind the skew covers that, and running it yourself.
(:require [re-frame.core :as rf]
[re-frame.ssr :as ssr]
[re-frame.ssr.ring :as ssr.ring]
[re-frame.ssr.ring.node :as node])
Assuming the app's :app/init event is registered, out/my-app-server/server.js is its server bundle, and MY_APP_BUILD_ID names that bundle's build:
(rf/init! ssr/adapter)
;; Launches the sidecar on the bundle, and fails here if it cannot.
;; `defonce`: each `renderer` under `:module` launches its own
;; sidecar, so a re-evaluated `def` would leave the last one running.
(defonce renderer
(node/renderer
{:module "out/my-app-server/server.js"
:entry "my-app/root"
:build-id (System/getenv "MY_APP_BUILD_ID")
:render-state {:app-db [:todos :session]
:runtime-db [:rf.runtime/routing]}}))
(ssr.ring/ssr-handler
{:frame {:initial-events [[:app/init]]}
:payload [:todos]
:renderer renderer})
;; …and when the system stops:
(.close renderer)
The renderer¶
renderer¶
- Kind: function
- Signature:
- Description: Returns a value for
ssr-handler's:renderer. It validatesoptsand builds one HTTP client at construction; per request it projects the frame's state under:render-state, POSTs it to the sidecar's/renderand returns the sidecar's body verbatim.- With
:module, construction also launches the sidecar: it copies the sidecar out of the jar to a temp directory, runs it withnodeon the bundle on a loopback port the OS picks, and waits for its ready line, refusing a bundle whose build id is not:build-id. It hands the sidecar:timeout-ms(as its deadline and its ceiling) and:admission-ms. A sidecar that exits is relaunched, after 100 ms doubling to 10 s while relaunches fail; requests that arrive while it is down fail as:rf.error/ssr-node-unreachable. - The returned fn is a
java.io.Closeable..closestops a sidecar it launched (SIGTERM, then a forced kill after 5 s) and is safe to call twice; a JVM shutdown hook stops one nobody closed. On a renderer built with:endpointit does nothing. - With a map policy, the handler's
:payloadpaths permit inside the render-state allowlist, so the markup and the payload agree on a permitted value. - The render module reads the state back with
re-frame.ssr.render-state/deserialize. A Fresco bundle's module built byre-frame.fresco.server/node-moduledoes that for you, so the bundle's:exports-varis one form (Write the render module). - A request waits at most
:timeout-ms+:admission-ms+ 600 ms, body included.
- With
- Options:
:entry(required) — the bundle entry to render; a non-empty string.:build-id(required) — a non-empty string equal to the server bundle's build id.:render-state(required) — what the render may see:{:app-db [<keys>] :runtime-db [<keys>]}, fail-closed allowlists of top-level keys with either slot optional, or(fn [frame-id] → {:rf/app-db {…} :rf/runtime-db {…}}). It is a policy separate from the handler's:payload. Every value must read back equal through EDN: no fn, record,#inst,#uuid,Ratio,BigDecimal,BigInt,BigInteger,Float,##NaN, or integer outside −(2^53 − 1) through 2^53 − 1, and no map or set holding two numbers the browser reads as one value (1beside1.0) (Two policies, and why they differ).- The map must name
:app-dbor:runtime-db(or both). Each present allowlist is a sequential of keywords; vectors, lists and lazy sequences are accepted. An empty allowlist, or an omitted partition, sends that partition as{}. An allowlisted key absent from the live frame is omitted. - Other policy keys and sets are invalid. There is no whole-partition keyword policy.
- A projector fn must return a map with only
:rf/app-dband:rf/runtime-db; either may be omitted, but a present partition must be a map with keyword top-level keys. Its result is validated for the wire, but the fn owns selection and redaction: the automatic classification projection applies only to the map policy.
- The map must name
:module— the path of the server bundle, resolved against the JVM's working directory; it must name a file. The renderer launches the sidecar on it. Naming:endpointas well is refused.:node— with:module: the Node executable, default"node"from the JVM'sPATH. It must be Node 24 or later.:isolates— with:module: the sidecar's worker threads, a positive integer; default 2.:endpoint— without:module: the absolutehttporhttpsURL of a sidecar you run; default"http://127.0.0.1:8148". A non-loopback URL is accepted; securing it is the operator's job.:args— root arguments sent as EDN, under the same value rule.:timeout-ms— the render deadline sent to the sidecar; a positive integer, default1000.:admission-ms— how long the sidecar may queue the request; a non-negative integer, default250. A sidecar launched with:moduleis given it; one you run needs the same--admission-ms.
- Errors:
- At construction:
:rf.error/ssr-node-renderer-opt-invalid(ex-data:opt,:got) for a malformed option, including:modulebeside:endpointand:nodeor:isolateswithout:module;:rf.error/ssr-missing-payload-policyor:rf.error/ssr-malformed-payload-allowlist, with:opt :render-state, for a missing or malformed:render-state. - At construction with
:module::rf.error/ssr-node-build-skew(ex-data:expected,:serving) when the bundle names another build;:rf.error/ssr-node-unreachable(ex-data:endpointnil,:ex-class,:ex-messagecarrying the sidecar's last stderr lines) when it cannot start — the bundle will not load,nodeis missing, or no ready line arrives within 30 s. - Per request, thrown at the render call. The handler projects each as
:rf.error/ssr-render-failed, with the id below in its:exception, so the request answers the 5xx error page and:on-erroris not called::rf.error/ssr-node-unreachable— no HTTP answer: connection refused, connect timeout, an I/O fault, or a launched sidecar that is down while it is relaunched (:endpointnil).:rf.error/ssr-node-deadline— the render missed its deadline;:observed-byis:sidecar(its504) or:jvm.:rf.error/ssr-node-refused— any other non-200; carries:statusand the sidecar's:refusalcode.:rf.error/ssr-node-build-skew— a200whosex-rf-ssr-buildheader is missing or is not:build-id.:rf.error/ssr-render-state-invalid— a projected value the EDN wire cannot carry, or a:render-statefn that returned a malformed envelope.
- At construction:
Defaults¶
renderer takes :timeout-ms and :admission-ms from these vars when you leave them out, and :endpoint too when there is no :module; it adds wire-margin-ms to derive its HTTP timeout.
| Var | Value | Meaning |
|---|---|---|
default-endpoint |
"http://127.0.0.1:8148" |
The sidecar launcher's default bind, used when :endpoint is absent. |
default-timeout-ms |
1000 |
Used when :timeout-ms is absent. It matches the launcher's --timeout-ms default. |
default-admission-ms |
250 |
Used when :admission-ms is absent. It matches the launcher's --admission-ms default; for a sidecar you run, raise both together. |
wire-margin-ms |
500 |
The fixed allowance the HTTP timeout adds for the wire itself. |
(http-timeout-ms opts) → msreturns the HTTP timeout each request carries::timeout-ms+:admission-ms+wire-margin-ms. Pass itoptswith both keys present, asrenderermerges them. The sidecar is given the smaller budget, so it refuses a late render before the JVM stops waiting.(validate-opts! opts) → optsruns the option checksrendererruns and returnsoptsunchanged, throwing the option errors listed underrenderer; it launches nothing. It does not merge the defaults itself, so pass itoptsmerged over them, asrendererdoes (:node"node"with:module,:endpointwithout).