Overview
Vue single-file components compiled to Rust render functions.
ferrovue lets a Rust server render a Vue 3 component's HTML with no JavaScript at run time, while the browser hydrates that HTML with the same .vue file. It has two halves, released together under the same version number:
| What it is | Where | |
|---|---|---|
| The compiler | Reads <script setup lang="ts"> components and writes one Rust module per component (TypeScript, runs on Node) | npm: ferrovue |
| The runtime | This crate: what the generated modules call, from escaping to <RouterLink> matching | crates.io: ferrovue |
Greeting.vue ──(ferrovue compiler, Node)──▶ greeting.rs ──(your Rust server)──▶ HTML ──▶ Vue hydrates itThe guarantee
ferrovue doesn't interpret templates. It takes what @vue/compiler-sfc compiles a template to for server rendering, straight-line _push calls plus a few @vue/server-renderer helpers, and translates that small, closed vocabulary into Rust that writes the same bytes. Vue still decides element structure, attribute order, whitespace and fragment markers.
A single differing byte would be a hydration mismatch in the browser, so anything the compiler can't translate exactly is a compile error that names the component and the construct. It never guesses. The repository's conformance suite renders every fixture with Vue's renderToString and with the generated Rust and compares the bytes; Vue then hydrates the recorded HTML with no mismatch. A randomised differential tester does the same with components nobody wrote by hand.
Quick start
The guide covers each step in detail; this is the whole path in brief.
1. Install both halves. The generated structs derive serde::Serialize, and the generated mod.rs has a test-only helper that reads props from JSON.
pnpm add -D ferrovue # or npm / yarn
cargo add ferrovue serde --features serde/derive
cargo add --dev serde_json2. Configure. ferrovue.config.json at the project root names the components and where the Rust goes. Everything in out is replaced on each run.
{
"components": "client/components",
"out": "src/generated"
}3. Write a component.
<!-- client/components/Greeting.vue -->
<script setup lang="ts">
defineProps<{ name: string; unread: number; note?: string }>();
</script>
<template>
<p class="greeting">Hello, {{ name }}!<b v-if="unread">{{ unread }} new</b><i v-if="note">{{ note }}</i></p>
</template>4. Generate.
pnpm ferrovue # writes src/generated/greeting.rs and src/generated/mod.rs
pnpm ferrovue --check # in CI: exits 1 if the committed modules are stale5. Include the modules and render. Declare the module once, with #[rustfmt::skip] so that ferrovue --check keeps comparing the files byte for byte:
#[rustfmt::skip]
mod generated;Each component's module has a Props struct, built with new and a setter per optional prop, and three ways to render, each with a twin that returns a render::Refused for props the render cannot write as Vue writes them:
use generated::greeting::{self, Props};
// `new` takes the required props; each optional one has a setter.
let props = Props::new("Ada", 3).note("welcome back");
// The markup alone, which the client never hydrates.
let html = greeting::html(&props).into_string();
assert_eq!(html, r#"<p class="greeting">Hello, Ada!<b>3 new</b><i>welcome back</i></p>"#);
// An island: the same markup in an element the client mounts on, with the props as JSON.
let island = greeting::island(&props).into_string();
assert!(island.starts_with(r#"<div data-island="Greeting" data-props='{"name":"Ada""#));
// Or straight into a buffer you already hold: no allocation of its own.
let mut page = String::from("<!doctype html><body>");
greeting::render(&mut page, &props);
// With props from a request, `try_render` returns what `render` would panic on, such as a date
// string browsers read differently, for the server to answer `400 Bad Request`.
greeting::try_render(&mut page, &props).expect("Greeting.vue renders any props");
// Every interpolated value is escaped.
let hostile = greeting::html(&Props::new("<script>", 0)).into_string();
assert_eq!(hostile, r#"<p class="greeting">Hello, <script>!<!----><!----></p>"#);A component with <style scoped> writes the same data-v- ids as Vue's server renderer, computed as @vitejs/plugin-vue computes them; see guide::scoped_styles.
On the client, the same component hydrates the page: mountIslands from ferrovue mounts every island, mountPage hydrates a layout and the parts a Page recorded in its slots, or the page's own Vue app hydrates the whole document. See guide::islands_and_hydration.
What the generated code calls
You rarely call most of this crate yourself; the generated modules do. The parts you do touch are the values a render needs besides its props.
| You build | To render a component that |
|---|---|
render::Slot, a generated Slots struct, scoped-slot closures | renders <slot> or <RouterView> |
router::Router (from the generated route_table::router()), then a router::Route per request | renders <RouterLink> or reads useRoute() |
A generated stores::Stores | reads Pinia state |
i18n::I18n (from the generated i18n::i18n(locale)) | calls $t or useI18n() |
page::Teleports | renders <Teleport> |
A generated provides::Provides, empty or holding what the client app gives app.provide | calls provide or inject, or renders one that does (guide::provide_inject) |
head::Head | calls useHead or useSeoMeta, or renders one that does (guide::head) |
html::BasicHtml, html::InlineHtml, html::Sanitised (the ammonia feature), or your own type implementing html::TrustedHtml | renders v-html |
date::JsDate, chrono::DateTime<Utc> (the chrono feature), or your own type implementing date::JsTime | takes a Date prop (guide::dates_and_prices) |
A prop declared Record<string, T> is a props::Record, which keeps JavaScript's order of keys.
and these, to assemble a page:
- [
html::Html]: a component applied to its props, borrowed (html(&props)) or held (into_html(props)), written into a buffer withhtml::Html::render_toor returned withhtml::Html::into_string; html::Html::hydratewith a [html::Hydrate]: an island that hydrates once visible, when the browser is idle, on interaction or when a media query matches;- [
head::Head::render]: the tags and attributes of the page's<head>,<html>and<body>; - [
page::state_script_into]: the stores' state for the client to hydrate from; loaders::loadand [loaders::Loads]: render components that await loaders, each loader started when Vue's server would start it, andloaders::Loads::script_intothe results for the client to hydrate from;page::holeand [page::split_holes]: render a layout once and stream its parts;web::HtmlStream(thestreamfeature): a page with holes as an HTTP body, each hole's content sent as soon as it is ready;- [
core::escape_into]: text written into your own markup as Vue would write it.
and, in tests, conformance! and testing::check_fixtures, which render an application's fixtures through its generated code and compare the bytes with the HTML recorded from Vue (guide::testing).
The rest are Rust twins of JavaScript and Vue routines that generated code calls so that it writes what Vue writes. Those a hand-written twin or page may call too are documented here:
- numbers:
core::push_int,core::push_number,core::Js,core::js_to_fixed,core::js_to_precision,core::js_to_exponential,core::js_round,core::js_sign,core::js_maxandcore::js_min; - URIs:
core::js_encode_uri_componentandcore::js_encode_uri; - Vue's own:
render::slot_into,attrs::attrs_into,attrs::passed_attrs_intoandrouter::query_into.
The others are #[doc(hidden)]: JavaScript's string methods counted in UTF-16 code units (js_length, js_slice, js_split and the rest the strings page lists), Number, parseInt, parseFloat and JSON.stringify (js_number, js_parse_int, js_parse_float, js_json_number, js_json_string), an optional value written as String() writes it (JsOr), dates as time values in UTC (js_date_parse, js_time_clip, js_to_iso_string and js_utc_full_year to js_utc_milliseconds), a Date prop handed to a child and written into data-props (js_time_of, js_date_prop, date_prop, opt_date_prop), a list's slice, at, reduce and toSorted (js_slice_range, js_slice_items, js_item_at, js_reduce, js_sorted, js_sorted_by_string, js_order), a list's join written escaped (escape_join, escape_join_ints, escape_join_numbers), text formatted into the page escaped (escaping, Escaping), and Vue's class, style, scope id, slot outlet, teleport, <RouterLink> and v-html helpers (class_into, class_object, class_names, style_text_into, merge_props, scope_attrs, slot_into_slotted, scoped_slot_into, scoped_slot_into_slotted, teleport_into, link_open, trusted_into). Only generated code calls them, so their signatures follow the version of generated code (the __compat! check at the top of a generated mod.rs) and may change in any release; the generated_code page says what is stable.
Feature flags
| Feature | Default | What it adds |
|---|---|---|
router | on | router::Router, router::Route, router::Link, router::Query, router::RouteDef and router::query_into, for components that render <RouterLink> or <RouterView> or read useRoute(), from the ferrovue-router crate |
i18n | on | The i18n module and i18n::I18n, for components that call $t or useI18n(), from the ferrovue-i18n crate |
maud | off | impl maud::Render for Html, so (greeting::html(&props)) can go straight into a maud::html! page |
stream | off | web::HtmlStream: a page rendered with holes as a stream of byte chunks, each hole filled by a future |
axum | off | stream, and IntoResponse for Html and web::HtmlStream, so a handler returns greeting::into_html(props) (guide::web_frameworks) |
actix-web | off | stream, and Responder for Html and web::HtmlStream |
ammonia | off | html::Sanitised, HTML cleaned by ammonia, to name as trustedHtml for v-html props (guide::escaping shows a policy of your own, with ammonia 4 as your own dependency) |
chrono | off | JsTime for chrono::DateTime<Utc>, to name as dateType for Date props, From<DateTime<Utc>> for date::JsDate, and TryFrom<JsDate> for DateTime<Utc>, whose error, date::DateOutOfRange, is an invalid date or one beyond chrono's ±262,142 years (guide::dates_and_prices) |
dioxus | off | The dioxus module: Html::to_element and impl IntoDynNode for Html, so {greeting::island(&props)} can go straight into Dioxus 0.7's rsx!, and dioxus::state_script; the guide's dioxus page shows a page |
An application with neither routes nor translations can turn the default features off (default-features = false) and builds neither ferrovue-router nor ferrovue-i18n. Generated code is the same either way: it names the router and translations only when the project configures them.
Crates
The runtime is four crates, released together at one version. This one holds each of the others whole as a module, core, router and i18n, and every item has one path, at the module that defines it, which generated code and this documentation use:
| Crate | What it holds |
|---|---|
ferrovue | This crate: what generated code calls, and the framework integrations |
ferrovue-core | core::escape_into, and JavaScript's numbers (core::push_int, core::push_number, core::Js, …), which the other three share |
ferrovue-router | router::Router and what goes with it (the router feature) |
ferrovue-i18n | i18n::I18n and what goes with it (the i18n feature) |
Rust version
ferrovue needs Rust 1.89 or later, the rust-version in its manifest, and CI tests that and the latest stable. The generated code uses let-chains, so the crate that includes it must use edition 2024.
Versions
Use the same version of the npm package and of this crate: generated code is written against the runtime of its own release. Byte-identical output also depends on Vue's own SSR compiler, so the npm package compiles with an exact @vue/compiler-sfc, and its fixtures are recorded from one version of Vue and of each integration. Your client may use any later patch of the same minor (vue ~3.5.45); the repository's README lists the range of every peer.
Links
- The repository, with the compiler's full documentation and a complete axum + Vite example: https://github.com/Kinsmir/ferrovue
- The compiler on npm: https://www.npmjs.com/package/ferrovue
- The guide, in this documentation.