Render fragments overview
Hand a piece of HTML to the server to redraw, without losing "what the user was in the middle of". The PHP version calls this renderBlade; the browser side is the same renderBlade.js, and on the server cg_component::runtime::render implements the same protocol.
What the problem looks like
Render fragments work like this: ask the server for a piece of HTML, then replace a container's whole innerHTML.
The upside is that page logic stays on the server and the browser doesn't need duplicate templates. There's only one downside, but it hurts:
User expands row 3 → clicks "Refresh list" → the whole block of HTML is replaced → the expanded state is goneBecause "which rows are expanded" used to be recorded in the DOM (a class, a hidden, a JS variable). Replace the DOM and the memory goes with it.
The fix here: never keep that state in the DOM to begin with.
Three roles
| Role | File | In one sentence |
|---|---|---|
| Object | resources/js/cg/renderObject.js | The permission boundary of a piece of DOM; its name is also the state namespace |
| State | resources/js/cg/state.js | A memory-only key-value store that lives for this load of this tab |
| Bindings | resources/js/cg/stateBind.js | Connect data-state-* to the state and update the DOM when values change |
Rendering itself is in resources/js/cg/renderBlade.js. None of it attaches to window, and all of it is synced from the PHP project: don't edit by hand.
On the Rust side:
| Role | Rust |
|---|---|
| Object attributes | runtime::render_object(name, &[Allow::…]) |
| Trigger attributes | runtime::render_trigger(...), runtime::render_auto(...) |
| Callable fragments | render::register(name, async fn(RenderInput) -> Result<Html>) |
| Endpoints | POST /{prefix}/render, POST /{prefix}/render/token |
| Render tokens | Issued by the share_head middleware, used up by render::consume |
Wiring (already done in the starter)
// src/routes/web.rs
cg_component::runtime::routes::<User>(route);
// src/app.rs, web middleware (after the session starts)
web.append(cg_component::runtime::share_head);
// src/app.rs, template functions
.with_views(|views, _| { cg_component::register(views); })<!-- resources/views/layouts/site.html -->
<html lang="en"{{ cg_html_attrs() }}>
<head>
{{ cg_head() }}
{{ vite("resources/js/site.js") }}
</head>share_headissues a render token on every GET page request and shares three meta tags with the templates: CSRF, render token and endpoints.cg_head()outputs those three meta tags;cg_html_attrs()outputsdata-cg-endpoints="m-…", telling the JS which meta to read.site.jscalls the runtime'sboot(): binds CGEvent,data-state-*and components, then fetches a fresh token and rendersdata-render-auto.
Your own layouts need these three too, or the console reports that the endpoint configuration is missing.
Five minutes to get going
1. Declare an object
Without this step, every attribute below is refused (the console says why).
let object = runtime::render_object("demoBox", &[Allow::StateRead, Allow::StateWrite]);
// → data-render-blade-object="demoBox" data-render-allow="state:read state:write""demoBox"is the name, and also the state namespace.- The second argument is the allow list:
Render,StateRead,StateWrite.
2. Add a toggle that remembers
<section{{ object_attrs }} data-state-init="open=false">
<button type="button" data-state-toggle="open">Toggle</button>
<div data-state-show="open">Whether this is shown is remembered</div>
</section>So far you've written no JS and haven't touched the server. Click the button → demoBox:open flips → the data-state-show subscribed to it changes hidden.
3. Make it survive a re-render
render::register("demoBox", |_input| async { Ok(Html::raw(render_box())) });
let object = runtime::render_object("demoBox", &[Allow::Render, Allow::StateRead, Allow::StateWrite]);
let refresh = Button::new("Ask the server for this block again")
.attributes(runtime::render_trigger("demoBox", RenderMethod::Components, Some("#demo-box"), &[]))
.render();<section id="demo-box"{{ object_attrs }}>
<button type="button" data-state-toggle="open">Toggle</button>
<div data-state-show="open">Whether this is shown is remembered</div>
{{ refresh }}
</section>The whole block of HTML is replaced → the new data-state-show reads open as soon as it's bound, finds it still true → the expanded state comes back by itself.
The allow list gained Render: triggering a render is a separate action and needs its own grant. How to write fragment functions is in Asking the server for fragments.
What one click does
User clicks the button
→ permit(button, "render"): find the nearest data-render-blade-object upwards; does its allow list have render?
→ renderBlade({ name, method, target, params })
→ POST /{prefix}/render (multipart: name, method, target, url, params[…], fingerprint, key)
state doesn't go along
→ Server: prefix right? → validate fields → use up the render token (already used: 419) → find the registered function (none: 422)
→ call it → in components mode compute the fingerprint → return { html, key: new token, fingerprint }
→ Browser: replace the target's innerHTML → run inline scripts → CGEvent.bind() → bindState() → renderBladeAuto()
→ The new nodes read back the old valuesThe key is the last two steps: after new content is inserted, bindings are always rescanned, so "replace the HTML" and "keep the state" don't conflict.