Alloy
Rendering pages
use smeltery::alloy::{self, Page};
#[derive(serde::Serialize)]
pub struct Activity {
pub accounts: u64,
}
pub async fn dashboard() -> Page {
alloy::render("dashboard") // resources/js/pages/dashboard.tsx
.with("greeting", "Welcome back")
.optional("stats", || async { Ok(42) })
.defer("activity", || async { Ok(Activity { accounts: 3 }) })
}
The component name is resolved by the client; in the kits it is the file under resources/js/pages/ without its
extension. In debug builds a component without a resources/js/pages/<name>.{tsx,jsx,vue,svelte} file logs a warning
once.
A typed page is a struct whose fields are the props:
#[derive(serde::Serialize, smeltery::Alloy)]
#[alloy("posts/index")]
pub struct PostsIndex {
pub titles: Vec<String>,
}
pub async fn index() -> PostsIndex {
PostsIndex { titles: vec!["Hello".into()] }
}
// `into_page()` adds lazy props to it:
use smeltery::alloy::{Component as _, Page};
pub async fn index_with_count() -> Page {
PostsIndex { titles: Vec::new() }
.into_page()
.defer("count", || async { Ok(0) })
}
The struct must serialize to a JSON object; #[derive(Alloy)] refuses enums, tuple and unit structs.
| Method | Sent |
|---|---|
.with(key, value) |
on full visits and on partial reloads that name it; serialized at once |
.with_lazy(key, f) |
like .with, computed only when it is sent |
.optional(key, f) |
only on partial reloads that name it (router.reload({ only: ['stats'] })) |
.defer(key, f), .defer_in(group, key, f) |
not on the first visit; listed in the page's deferredProps, and the client asks for it right after (<Deferred data="activity">); one request per group |
.merge(key, value), .prepend(key, value), .deep_merge(key, value), .match_on(key, "id") |
like .with, and the client merges the value into the one it has (infinite lists) |
.always(key, value) |
on every response, partial reloads included |
Lazy props are FnOnce closures (Send + 'static) returning a future of smeltery::Result<T>; they capture what
they need (a Db clone); the included ones run concurrently, and an error fails the request. A partial reload names
props with X-Inertia-Partial-Data / -Except (dot paths reach into values, user.name); a reload of another component gets
the full page. .status(code), .encrypt_history(bool) and .clear_history() are page options.
Every prop is public: the page is readable in the HTML of a first visit and in the JSON of every visit. Never pass models that hold secrets (password hashes, tokens), session values or the CSRF token; send a struct with the fields the page needs. A client can ask for any prop a page declares, optional and deferred ones included, so authorization belongs to the route's middleware and the controller.