FerriteDocs

Ferrite Alloy

A Rust web framework for Ferrite: URL routing, tag-invalidated cached loaders, replay-safe actions, HTML documents, and an optional typed React front end.

Ferrite Alloy is a web framework for Ferrite applications. You write routes, data loaders, mutations, and your HTML shell in Rust, and Alloy handles the plumbing underneath: URL matching in the Next.js App Router grammar, tag-invalidated caching, replay-safe actions keyed by idempotency key, and server-rendered document metadata. If you attach a React front end, the CLI’s built-in contract generator turns your Rust declarations into TypeScript types, a fetch client, and TanStack Query hooks.

Alloy arrives as the alloy source package vendored into src/alloy/. Your Cargo.toml keeps one dependency (ferrite-app), the framework passes the same source gate as your own code, and it runs strictly with the handles your manifest declares.

To scaffold and run a working app locally:

ferrite new alloy my-app --with-tests
cd my-app
ferrite dev

When to use it

Use Alloy for multi-page web applications that need cross-visitor cached loaders, replay-safe mutations, cross-route cache invalidation, and crawler-readable document metadata.

  • For an API, a background worker with no pages, or a single page with one form or counter, write plain Functions.
  • If you prefer writing pages as .rsx templates over assembling HTML strings in Rust, use Rsx; Alloy versus Rsx compares both frameworks side by side.
  • No JavaScript runs on the server and every reply is one complete buffer, so pages render from Rust or in the browser (no server-side React, React Server Components, or HTTP streaming).
  • Moving an existing Next.js app requires keeping the React components on the client and rewriting all server logic as Rust loaders and actions; see Next.js Adapter for the mapping.
  • Alloy loaders read the key-value store by exact key and by prefix scan.

What the CLI gives you

The ferrite CLI supports Alloy in three setups.

ferrite new alloy my-app scaffolds a server-rendered Rust application with two pages, one form action that writes to the store, one cached loader shared by both pages, a seed function, and both alloy and sha256-hmac already vendored. No JavaScript build is involved; Rust renders each page into the HTML shell. The Quickstart walks through this layout end to end.

ferrite add alloy installs the same vendored runtime into an existing Ferrite application started from another template.

You can also declare routes and actions in Rust under app/ and serve a Vite React front end from web/. Whenever app/, web/package.json, and src/alloy/mod.rs all exist, ferrite dev runs the CLI’s built-in contract generator, executes the front end’s npm build, and packs the compiled assets into a Rust module (src/site_assets.rs). The generator writes both the Rust route registry and contract types and the TypeScript types, fetch client, TanStack Query hooks, and route table, so changing a loader’s signature in Rust causes a TypeScript compile error on the next build. No starter template creates this layout; follow Add a typed contract and a React front end to add app/ and web/ to a scaffolded app.

Three tools used in Ferrite’s own ported applications are outside the CLI distribution: the Next.js adapter (@alloy/next), the alloy check seam checker, and the ported example applications themselves. Next.js Adapter and Testing & Gates cover what each does and what to use in your own project.

Feature tour

FeatureHow Alloy does it
RoutingApp Router patterns ([id], [...rest], groups) in a deterministic Rust route table; see Routing
RedirectsDeclarative redirects with parameter substitution, resolved before route matching
Data loadingCached Rust loaders with three freshness windows, tags, and named cache profiles; see Data & Caching
InvalidationTag epochs: advancing a tag is one write regardless of how many cached entries carry it
MutationsReplay-safe actions with durable result records, so a repeated key returns the first admitted answer; see Actions & Effects
Side effectsAn outbox committed atomically with the action and drained at least once
DocumentsAn HTML shell with Rust-substituted metadata and embedded loader payload; see Documents & Assets
MetadataTitle templates, parent merging, theme colors, icons, and robots, rendered server-side where crawlers read them
Draft modeAn HMAC cookie (__Host-alloy_draft) that bypasses the cache on both reads and writes
SecurityOrigin checks on actions, CSP nonces, per-route response headers, and mount-point authority

Where these docs call a record durable, an acknowledged write or outbox intent survives a crash, a process restart, and a redeploy of your application.

The platform facts it is built around

Four Ferrite platform constraints dictate Alloy’s runtime design.

Every HTTP reply is a single buffer (IngressReply.body is one Vec<u8>), so documents cannot stream. Alloy substitutes metadata and loader payloads into an HTML shell, and slower data loads as deferred segments fetched after first paint.

An atomic envelope’s commit_n_if guards a single key. To advance multiple cache tags atomically alongside an action’s replay marker, Alloy runs a guarded read-modify-write over one shared version key and packs the guard, replay marker, tag epochs, and action effects into one 64-entry envelope.

The key-value store’s TTL is a hard expiry cliff. Alloy maps only the expire window onto the store TTL and carries stale and revalidate inside the cache envelope to evaluate at read time.

Object storage keys are chosen by the caller. Alloy seeds each packed asset under its served path and skips writes whose stored bytes already match. Long-lived caching relies on content-hashed filenames: hashed assets are served immutable for one year, HTML documents and the stable /assets/entry.js and /assets/entry.css aliases are served no-cache for ETag revalidation on every request, and all other assets are served public, max-age=300 before ETag revalidation.

What the runtime owns

Each module under src/alloy/ owns one concern of the vendored runtime:

ModuleWhat it owns
appRequest loop, AlloyApp trait, action dispatch with mount-point authority, and reply helpers
routerRoute patterns, parameter extraction, specificity ranking, and declarative redirects
tagsTag-epoch table: single-write invalidation over one guarded envelope in an APE (atomic envelope) binding; see Transactions
cacheCache envelopes, three freshness windows, named profiles, and witness validation
ctxCached-loader path: L1 and L2 lookup, stale-while-revalidate, single-flight, and draft bypass
actionsReplay-safe mutations: idempotency markers, action stamps, and outbox intents
outboxOutbox drain: durable discovery, shard leases, and at-least-once delivery
pageBounded prefix scans and declared invalidation budgets
sagaCross-key writes as declared phases with durable resume
activityEphemeral user-held state (such as drafts) expired through the store TTL
documentShell templates, <head> metadata, CSP nonces, and script-safe payload injection
segmentsDeferred portions of a loader payload fetched after first paint
draftThe __Host-alloy_draft cookie that bypasses the cache for previews
assetsPacked asset archive, digest manifest, and object-storage seeding
errorTyped error surface and RFC 9457 problem rendering

What is deliberately not claimed

Alloy provides no user accounts or sessions of its own. Its origin check blocks cross-site form posts, while your admit implementation decides which callers hold an action’s mounted authority and which records they may read or modify. Add sign-in with the auth package as shown in Sign-in for Alloy applications, and keep per-visitor data out of shared cache keys (see Cached loaders and per-visitor data).

No JavaScript runs on the server. When you add a React front end, React renders in the browser, and any server-rendered HTML body comes from what your Rust code writes into the shell.

Delivery guarantees depend on the effect class. APE writes committed in the same envelope as the replay marker apply exactly once, store writes carrying an action stamp apply effectively once while uncontended by another writer, digest-keyed object writes are naturally idempotent, and queue, egress, and notification outbox intents deliver at least once with a carried idempotency key. One action commit shares a 64-entry APE envelope: the version guard and replay marker use 2 entries; the remaining 62 are shared by the action’s APE writes, tag advances, outbox intents, and pending-index entry. Actions & Effects details each class.

Multi-tag atomicity holds within a single APE version guard and does not span shards.

Next: Alloy Quickstart.

Ferrite counts page views on this site. Privacy · Terms