MARY.WIN

RUSTY — INERTIA — CHAPTER 08

The Root
Document

Chapter 8 of 10 in Inertia on Rust, draft chapters on the Inertia adapter for Rust.

The first request to an Inertia application gets HTML. Every request after it gets JSON. In this chapter we'll write the function that produces that one HTML document, see what the crate puts inside it, and look at what the server-side rendering option changes.

This chapter assumes Chapter 2. Server-side rendering is described from the source and was not run.

One document

The root view was a function of nine lines, and it was the only HTML the server would write. It put the title in, joined the head, and placed the body. After the first response it was not consulted again; every later page arrived as JSON and React did the rest.

Sometimes a page needs something in the document that isn't a prop: a title, a meta tag, a script. Thankfully, the document is your own function. Take a look at Example 8-1.

Example 8-1. A root view with a title

pub fn root_view(view: &inertia::View<'_>) -> String {
    let title = view.data.get("title").and_then(|title| title.as_str()).unwrap_or("Ledger");

    format!(
        "<!DOCTYPE html>\n<html>\n<head>\n<title>{title}</title>\n{head}\n</head>\n<body>\n{body}\n</body>\n</html>\n",
        head = view.head.join("\n"),
        body = view.body,
    )
}

pub async fn about(inertia: Inertia) -> inertia::Response {
    inertia.render("About", ()).with_view_data("title", "About Ledger")
}

There are three new pieces here. A root view is any function from a View to a String, registered with Config::root_view. view.body is markup the crate has already built. with_view_data attaches data that only the root view sees; the page does not receive it.

Example 8-2 is the response to a first visit.

Example 8-2. The first visit

<!DOCTYPE html>
<html>
<head>
<title>About Ledger</title>
</head>
<body>
<script data-page="app" type="application/json">{"component":"About","props":{"errors":{},"app_name":"Ledger","auth":{"user":"Mary"}},"url":"\/about","version":"1","sharedProps":["errors","app_name","auth"]}</script><div id="app"></div>
</body>
</html>

As you can see, the body is two elements: a <script> holding the page object as JSON, and an empty <div> for the app to mount in. The title is in the document and absent from the page's props.

You can also change the mount element's id, which is app by default, with Config::root_id.

If you configure no root view at all, the crate uses a bare document with a charset and a viewport tag. If you're unsure whether to keep that, I'd write your own on the first day; the default has no script tag for your bundle, so your JavaScript would not load.

Server-side rendering

You can also have first visits rendered on the server. Config::ssr takes a gateway, and the crate ships one, HttpGateway, that sends the page to an Inertia SSR server over HTTP. Its default address is http://127.0.0.1:13714. Give it a Vite hot file with hot_file, and while that file exists it renders through the Vite dev server instead.

With SSR on, view.head holds the head elements the renderer produced and view.body holds rendered markup. When a gateway returns nothing, the page falls back to client-side rendering.

What this chapter leaves open

So the first response is a document you wrote, with the page inside it as JSON. That JSON is in the HTML source of every first visit, shared props included. What is in your auth prop? Is every field in it something that user's browser should hold?

Testing

Example 8-3 makes a request with no Inertia header. It fails if the title is missing from the document or present in the props.

Example 8-3. The document and the page inside it

let request = http::Request::get("/about").header("host", "localhost").body(Body::empty()).unwrap();
let reply = send(&app(), request).await;

assert!(reply.body.contains("<title>About Ledger</title>"));
AssertablePage::from_body(&reply.body).component("About").url("/about").missing("title");

TL;DR

A root view is a function from a View to the HTML of a first visit. The crate supplies the body: a JSON <script> with the page object and the mount element. with_view_data passes values to the root view without making them props. Config::ssr with HttpGateway renders first visits through an SSR server, and a gateway that returns nothing leaves the page to render on the client.

The sources.

Each line is a claim this chapter makes and where to look. Checked on 2026-11-29 against inertia-laravel-rust at commit 3ccc126.

CLAIM × WHERE — 9
  • A root view is any Fn(&View) -> String; it renders the document of a first, full visitR1 · src/view.rs L7-L34; src/config.rs L85-L89
  • View fields: page, head, body, data, ssrR1 · src/view.rs L36-L50
  • with_view_data adds data for the root view only; the page does not receive itR1 · src/response.rs L72-L77
  • The body is a JSON script with data-page and an empty mount div with id apprun · test output a_first_visit_gets_the_root_document
  • root_id sets the mount element id, app by defaultR1 · src/config.rs L95-L99
  • The default root view has a charset and viewport meta and no script tagR1 · src/view.rs L52-L64
  • Config::ssr takes a Gateway; a gateway returning None falls back to client-side renderingR1 · src/config.rs L117-L121; src/ssr.rs L29-L36
  • HttpGateway: default URL http://127.0.0.1:13714; renders through the Vite dev server while the hot file existsR1 · src/ssr.rs L49-L54, L76-L100
  • With SSR, head holds rendered head elements and body holds rendered markupR1 · src/view.rs L41-L45; src/ssr.rs L20-L27

R1: inertiajs/inertia-laravel-rust @3ccc126a24c8fb5bddb140876a1825292c0d216c
run: Observed by running the chapter listings on 2026-11-29; the code that produces it was not read