Roc Solid
Markup in Roc · pages on a native server · components in WebAssembly

The platform · the browser

The components and the engine in WebAssembly, and a page of JavaScript

The browser runs the same components the server renders: every page, with its layouts, compiled to one WebAssembly module with rocx-engine, the Rust engine every target runs. Your Roc decides what each hole shows and what each handler does. The engine tracks what reads what and runs a hole again when what it read changes, inside the wasm; a page of JavaScript applies what changed to the DOM. You write no JavaScript.

The parts#

web/engine.js

JavaScript, and nothing that decides. Claims the server's nodes for the engine's as the page hydrates, applies each batch of changes to the DOM, and passes events in. Does what only a browser can: requests and streams, timers, history, scroll and focus, animations, drags by touch.

ops and events

One wasm module

engine/ (rocx-engine)

Rust, inside the wasm: every hosted function, the signals, memos, effects and computations, which component owns what, a document of its own, navigation, forms and channels. Roc's memory. The same crate runs on phones and renders on the server.

The app, on web/platform

Roc: your components, their markup with holes as closures, escaping and safe URLs, and endpoints as requests.

Markup reaches the engine as JSON: elements, text, and holes, each a Roc closure the engine keeps boxed under an id. Running a hole calls the app's run with its box, and the answer is JSON again: a text, an attribute's value, or markup. The engine builds a document of its own from it, and after each flush sends the page what changed as one batch of operations. The server renders the same JSON with the same engine (pages, rendered on the server). Lists are cheaper: once the page is up, a .map's first row sends its skeleton (its elements, and the attributes and text written in the file), which the page keeps and copies; each later row sends only what fills the copy (its holes, handlers and values). The bootstrap, /rocx/start.js (web/start.js with web/engine.js), ends every page: it loads the wasm from beside itself, and hydrates the document.

What components can use#

Components use the browser platform's modules, which the server platform has too (the build copies them):

Server, Middleware and OpenApi are there too, so route files compile on both sides. Roc compiles only what a program reaches, so server-only code costs the wasm nothing.

What an app gives the platform, and what the platform gives the host:

web/platform/main.roc, in part
platform ""
	requires {
		main! : {} => Html
	}

main! is the app's root, which rocx writes into the browser's copy (Generated/Root): a whole document, the page at the browser's path inside its layouts. The platform gives the host two functions: mount the root, and run a kept box. The browser answers no requests: every endpoint a component reads is the server's (in the tests, Node answers reads from the answers the page recorded). The rest of the header is what Roc may ask of the engine, each a Rust call inside the wasm: boxes, signals, memos, fetches (an endpoint's read is a GET), streams, channels, contexts, where the page is (Router.location!), and so on. What the engine can't do itself (a request, a timer, the history) it asks of the page's JavaScript.

The whole header: web/platform/main.roc
web/platform/main.roc
## The browser's platform: an app's page, Solid 2.0-style components, in one wasm module the
## browser loads (web/start.js and web/engine.js run it: the wasm holds rocx-engine, web/host). A component runs once; its markup's holes
## are computations the runtime runs again when what they read changes; an async read (an
## endpoint's answer, a memo waiting for one) aborts its computation until it is in, and the
## nearest <Loading> or <Errored> shows instead.
##
## - `main!`: the app's root, the page (`rocx generate --client` writes it: `Generated/Root`), which
##   the browser hydrates from the document the server rendered. The server platform
##   (`server/platform`) renders the same components natively, and answers the endpoints they read;
##   the browser's copy of an app answers none.
##
## The modules both platforms have are written here, and the server platform's build copies them:
## the components' (`Html`, `Solid`, `Store`, `Repeat`, `Transition`, `Loading`, `Errored`, `Context`,
## `Call`, `Router`, `Search`, `Leave`, `Submission`, `Submissions`, `Title`, `HttpStatusCode`,
## `Browser`) and HTTP's (`Server`, `Endpoint`, `FormBody`, `Channel`, `Middleware`, `Events`,
## `OpenApi`). Roc compiles only what a program reaches, so what
## only the server uses (`Middleware`, `OpenApi`) costs the browser's wasm nothing. `Time` is the
## browser's own, under the server's names: the clock, for handlers, effects and timers.
platform ""
	requires {
		main! : {} => Html
	}
	exposes [Browser, Call, Channel, Context, Endpoint, Errored, Events, Html, HttpStatusCode, Leave, Loading, Middleware, Navigate, OpenApi, Repeat, Router, Search, Server, Solid, Store, Submission, Submissions, Time, Title, Transition]
	packages {}
	provides { "roc_mount": mount_for_host!, "roc_run": run_for_host! }
	hosted {
		"roc_action": Host.action!,
		"roc_app": Host.app!,
		"roc_before_leave": Host.before_leave!,
		"roc_boundary": Host.boundary!,
		"roc_browser_ask": Host.browser_ask!,
		"roc_browser_call": Host.browser_call!,
		"roc_cleanup": Host.cleanup!,
		"roc_clear_timer": Host.clear_timer!,
		"roc_context_find": Host.context_find!,
		"roc_context_get": Host.context_get!,
		"roc_context_value": Host.context_value!,
		"roc_effect_new": Host.effect_new!,
		"roc_effect_options": Host.effect_options!,
		"roc_event_value": Host.event_value!,
		"roc_fail": Host.fail!,
		"roc_fetch": Host.fetch!,
		"roc_flash": Host.flash!,
		"roc_for_new": Host.for_new!,
		"roc_head": Host.head!,
		"roc_host_live_publish": Host.live_publish!,
		"roc_interval": Host.interval!,
		"roc_is_pending": Host.is_pending!,
		"roc_is_routing": Host.is_routing!,
		"roc_keep": Host.keep!,
		"roc_leave_prevent": Host.leave_prevent!,
		"roc_leave_retry": Host.leave_retry!,
		"roc_listen": Host.listen!,
		"roc_location": Host.location!,
		"roc_memo_done": Host.memo_done!,
		"roc_memo_get": Host.memo_get!,
		"roc_memo_last": Host.memo_last!,
		"roc_memo_new": Host.memo_new!,
		"roc_navigate": Host.navigate!,
		"roc_now_millis": Host.now_millis!,
		"roc_now_nanos": Host.now_nanos!,
		"roc_optimistic_set": Host.optimistic_set!,
		"roc_own_writes": Host.own_writes!,
		"roc_problem": Host.problem!,
		"roc_read_at": Host.read_at!,
		"roc_refresh": Host.refresh!,
		"roc_request": Host.request!,
		"roc_resync": Host.resync!,
		"roc_retry": Host.retry!,
		"roc_revalidate": Host.revalidate!,
		"roc_row_get": Host.row_get!,
		"roc_row_index": Host.row_index!,
		"roc_row_last": Host.row_last!,
		"roc_row_set": Host.row_set!,
		"roc_say": Host.say!,
		"roc_selector_is": Host.selector_is!,
		"roc_selector_new": Host.selector_new!,
		"roc_signal_get": Host.signal_get!,
		"roc_signal_latest": Host.signal_latest!,
		"roc_signal_new": Host.signal_new!,
		"roc_signal_set": Host.signal_set!,
		"roc_status": Host.status!,
		"roc_submission": Host.submission!,
		"roc_submission_clear": Host.submission_clear!,
		"roc_submission_drop": Host.submission_drop!,
		"roc_submission_retry": Host.submission_retry!,
		"roc_submissions": Host.submissions!,
		"roc_submit": Host.submit!,
		"roc_subscribe": Host.subscribe!,
		"roc_template": Host.template!,
		"roc_timeout": Host.timeout!,
		"roc_title": Host.title!,
		"roc_tracking": Host.tracking!,
		"roc_untrack": Host.untrack!,
	}
	targets: {
		inputs_dir: "targets/",
		wasm32: {
			inputs: ["host.wasm", app],
			output: Shared,
			exports: ["rocx_start", "rocx_hydrated", "rocx_describe", "rocx_event", "rocx_listens", "rocx_drag_of", "rocx_value", "rocx_submit", "rocx_link", "rocx_popped", "rocx_navigate", "rocx_would_stop", "rocx_document_request", "rocx_answered", "rocx_failed", "rocx_heard", "rocx_fired", "rocx_frame", "rocx_left", "rocx_scratch", "rocx_result", "rocx_result_room", "rocx_call", "rocx_live", "rocx_allocations", "rocx_counts", "rocx_row_key", "rocx_enhanced", "rocx_live_wait", "rocx_pending", "rocx_notes_kept", "rocx_notes_room", "rocx_answers_kept"],
			initial_stack_size: 1048576,
		},
		# The phone (D-063's spike): the app alone, as an archive that native/android/build.sh links
		# with the native host into an Android library (the NDK's clang). Roc has no Android target
		# yet; its static Linux objects for arm64 (musl: no C library in them) link as Android's.
		# And the Mac's, for the native host's tests on the Mac (native/mac).
		arm64musl: {
			inputs: [app],
			output: Archive,
		},
		arm64mac: {
			inputs: [app],
			output: Archive,
		},
	}

import Host
import Html
import Browser
import Solid
import Store
import Repeat
import Loading
import Errored
import Call
import Channel
import Server
import Context
import Endpoint
import Middleware
import Events
import OpenApi
import Router
import HttpStatusCode
import Transition
import Submission
import Submissions
import Time
import Title
import Search
import Leave
import Navigate

## The app's markup, its holes registered with the runtime, as JSON (`Html.serialize!`).
mount_for_host! : {} => Str
mount_for_host! = |{}| Html.serialize!(main!({}))

## Runs a computation, a handler or a fallback the runtime keeps; its answer as JSON.
run_for_host! : Box(() => Str) => Str
run_for_host! = |runner| {
	run = Box.unbox(runner)
	run()
}

One click, step by step#

You press + on the counter on the front page. Then:

  1. JavaScript The document's listener for clicks (the engine said its handlers take them) finds the engine's node under the click and passes the click in.
  2. Rust The engine bubbles it through its own document to the button's handler, and runs its box.
  3. Roc || set_count!(count!() + 1) runs: count!() asks the engine for the signal's value (3), and set_count! hands it the next one (4), boxed.
  4. Rust When the handler returns, its writes apply: the signal holds 4, and the three computations that read it are due: the <output>'s text, the negative class, and the Reset button's disabled.
  5. Roc Each runs again: the text is "4", the class still off, disabled still false.
  6. Rust Only the text changed, so only its node changes in the engine's document: an unchanged value isn't patched. (Reset to 0 would patch the button too.)
  7. JavaScript The page applies the flush's one change: the text node is set.

Hydration#

Hydration is how the browser takes over the server's page without building it again:

  1. JavaScript The page ends with a script holding its endpoint reads' answers, then the bootstrap, /rocx/start.js. It fetches the app's wasm (475.2 KB with brotli for this site, one module for every page, the engine in it) from beside itself, compiles it, and hydrates the document.
  2. JavaScript It reads the answers and takes their script out of the page.
  3. Roc The root runs once: it reads where the page is (Router.location!) and renders that page in its layouts, a whole document, each component once, where it is rendered. A read whose answer the page has (under GET and its URL) gets it from there.
  4. Rust The engine builds its document from that, as on a fresh start, but the page doesn't apply it.
  5. JavaScript Instead it walks the engine's document beside the server's nodes, from the doctype and <html> down, claiming each node for the engine's. The server's HTML comments mark each hole, list row and if branch (<!--x-->…<!--/x-->). Nothing is fetched or built again.
  6. JavaScript Elements a browser extension put straight in <body> or <head> (a password manager's popup, a dark theme's style) are passed over, and stay. Any other mismatch (the HTML parser moved a <div> out of a <p>) makes the runtime warn, naming the node and its place, tell the page (a rocx:mismatch event, for error reporting), and make <body>'s children again from the engine's document. <head> is left alone: rendering it again would rerun its scripts and flash its styles. To investigate one, localStorage.setItem("rocx:strict", "1") makes the next load throw at the mismatch instead.
  7. JavaScript The bootstrap tells the page it is done with a rocx:ready event, carrying how long hydrating took. From then on, a read the page has no answer for is a GET of the endpoint's URL.

What it never hydrates#

<NoHydration> (Html.no_hydration in Roc) marks markup only the server renders: prose, listings, anything that never changes in the browser. The server runs it once and writes it between <!--nohydration--> and <!--/nohydration-->, with no markers inside, leaving its reads out of the page's answers. In the browser's copy of a .rocx file (in build/client, which rocx writes) the region has no children, and imports only they used are gone, so none of it is in the wasm. Hydration skips the nodes between the markers, never building, claiming or patching them; a <body> rendered again keeps them.

Nothing inside runs in the browser, so rocx refuses a handler, a key or a transition written there, at its line, and the server refuses to render a handler a component put there: a 500 naming the element, rather than a button that silently does nothing. This page is all one <NoHydration>, so its prose costs the wasm nothing.

After hydration, a link to another of the app's pages doesn't reload. The page fetches that page's HTML document, as a reload would, and the engine renders it from the document's answers with the wasm it has (every page is in the one module). Layouts both pages share stay, the address goes into the history, and scroll, focus and title change as on a reload. Navigating without a reload lists what an app controls.

Waiting, in wasm#

A component reads a memo that waits for the server (an endpoint's answer, say) as a plain value: no async in Roc, and no JSPI. Until the answer is in, the read ends the call into Roc with a throw through the wasm frames that asked. JavaScript catches it around the call, puts Roc's stack pointer back, and the engine runs the computation again when the answer comes, freeing what the stopped run allocated (memory). The first time, a <Loading> above shows its fallback; after that, what was shown stays until the new value is in (waiting and failing).

The numbers#

Measured by site/build.mjs in Node v24.22.2: this site's wasm, built three ways (the other two on 2026-11-29, when node site/build.mjs last compared them). "Rendering" is the wasm rendering the front page as a fresh page, from the server's answers, with no DOM: every component and hole outside its <NoHydration>s, once, the median of 50.

Built withRawgzipbrotliRendering
--opt=size1868.8 KB688.6 KB515.9 KB778 µs
--opt=speed3858.7 KB1018.9 KB681.9 KB1385 µs
--opt=size --specialize=yes this site1907.5 KB644.3 KB475.2 KB651 µs

Specializing Roc's lambda sets makes this site's wasm the smallest download; --opt=speed renders a little faster and is the biggest raw. Compiling this site's wasm took 1.7 ms in Node. The page's JavaScript, start.js with engine.js minified as rocx writes it into the browser's copy, is 24.6 KB gzipped. A release or a static copy (rocx build, --static) builds an app's wasm with only the engine's parts its code uses (navigating in place, live updates, transitions, drags, pointers, frames, forms, files, templates) and shakes start.js to what that wasm reaches: examples/hello downloads 135.0 KB with brotli, this site's front page 493.5 KB, all of it. The showcase measures itself as it runs (frames a second, and what each costs, Roc and patches together); js-framework-benchmark measures the platform beside Solid, Leptos, Joy, React and hand-written DOM code.

Ran on

Machine
MacBook Pro (Mac17,2)
Processor
Apple M5 (arm64)
Cores
10 (4 performance, 6 efficiency)
Memory
32 GB
Storage
SSD, APFS
System
macOS 27.0.1
Power
battery

Roc's floating-point code weighs most in the wasm. Building with and without it, parsing a float from a string costs about 18 KB compressed, printing one about 12 KB, and sine and cosine about 5 KB. So the page sends frame times as whole microseconds, the showcase's flock prints its positions as whole numbers, and a bird's heading is a random vector scaled by a square root, one wasm instruction.

Memory#

A page keeps its wasm instance while it is open, so memory has to come back. Roc's memory and the engine's are a Rust crate, web/memory, which the engine uses on a phone too. It allocates power-of-two slots, as Zig's allocator for wasm does, and counts what is live. A box the engine keeps (a hole's closure, a signal's value) is released when what made it goes: a row dropped, an if switching branch, a value replaced. A run that stops to wait skips the frees Roc would make on the way out, so each run's allocations are noted, and what a stopped run still held is freed once the engine lets go of what it made. The tests click, type, add and drop rows, wait for the server, and render pages over and over, checking that live allocations don't grow.