SSR migrated to Rust

This commit is contained in:
2026-06-04 21:37:06 -04:00
parent ae684a36cb
commit 587be8c4ab
12 changed files with 2825 additions and 472 deletions

View File

@@ -0,0 +1,3 @@
node_modules/
package-lock.json
bundle.js

View File

@@ -0,0 +1,7 @@
import { createElement } from "react"
// A trivial component: props in, element out. The keystone only needs to prove
// a real React tree renders to HTML inside a bare JS context.
export function Hello({ name }) {
return createElement("div", { id: "greeting" }, `Hello, ${name}!`)
}

View File

@@ -0,0 +1,8 @@
import { renderToStaticMarkup } from "react-dom/server.browser"
import { createElement } from "react"
import { Hello } from "./Hello.js"
// The bundle exposes one global the embedded engine calls. No module system at
// runtime — the engine receives a bare script that defines `renderApp`. This is
// the production shape in miniature: build-time bundle, runtime eval.
globalThis.renderApp = (props) => renderToStaticMarkup(createElement(Hello, props))

View File

@@ -0,0 +1,7 @@
{
"dependencies": {
"esbuild": "^0.28.0",
"react": "^19.2.7",
"react-dom": "^19.2.7"
}
}

View File

@@ -0,0 +1,29 @@
// Proxy for the embedded-V8 runtime: a bare global context with no Node
// builtins. Load the IIFE bundle (which assigns globalThis.renderApp) and call
// it. What renders here renders in rusty_v8 — the engine swaps, the contract
// (bundle defines a global render fn over a bare context) does not.
const fs = require("fs")
const vm = require("vm")
const code = fs.readFileSync(__dirname + "/bundle.js", "utf8")
// The minimal host globals React's bundle touches at init / sync render. The
// rusty_v8 engine must provide the same set — this list is the spec for it.
const sandbox = {
console, setTimeout, clearTimeout, queueMicrotask, MessageChannel, performance,
TextEncoder, TextDecoder,
}
sandbox.globalThis = sandbox
vm.createContext(sandbox)
vm.runInContext(code, sandbox)
const html = sandbox.renderApp({ name: "World" })
console.log("RENDERED:", html)
const expected = '<div id="greeting">Hello, World!</div>'
if (html !== expected) {
console.error("MISMATCH — expected:", expected)
process.exit(1)
}
console.log("OK — React bundle renders in a bare JS context (V8 proxy)")