Inertia lets an application keep its server-side routes and handlers and still build its pages in React, Vue or Svelte, with no JSON API between the two. inertia-laravel-rust is the server half of that arrangement for Rust. In this introduction we'll define the words the chapters lean on (Inertia, page component, props, page object, adapter, struct, handler) and look at one handler from the crate's own example.
This introduction covers vocabulary and one listing. It does not cover installing the crate or setting up a React build. It assumes you have written, in some server framework, a route that returns a view.
What Is Inertia?
The link to /invoices/8 expected what links had always gotten, which was a new document. It was an anchor with an href, and that was all it knew about itself. The click never reached the browser's navigation. A script took it, sent the request with one extra header, and received a few lines of JSON and no document at all.
Sometimes you want React for the page and your server's routing, handlers and middleware for everything else, and the usual price is a JSON API that you design, version and keep in step with both sides. Thankfully, there's an arrangement without one. Inertia is a client-side library, and a protocol between that library and your server, in which the server answers a page visit with the name of a JavaScript component and the data for it.¹
Full definition Inertia
- Kind
- A client-side routing library, and the protocol it speaks with a server.
- Parts
- Two parts. The client library runs in the browser. An adapter implements the protocol on the server.
- Behavior
- Answers a page visit with the name of a page component and its props, in place of a server-rendered view.
- Boundary
- Not a JSON API that you design. The server keeps its routes, handlers and middleware, and the protocol fixes the shape of the response.
- Instance
- A visit to
/invoices/7carryingX-Inertia: truegets back a page object whosecomponentisInvoices/Show. - Analogue
- Returning a Blade view from a controller, except the view is a JavaScript component and its data arrives as JSON. This comparison is the author's.
Three terms come with it:
- A page component is a React, Vue or Svelte component that is a whole page. The server refers to it by a string, such as
Invoices/Show. - Props are the data the server sends to that component.
- A page object is the JSON that describes one page. Take a look at Example 0-1.
Example 0-1. A page object, trimmed to its four required fields
{"component":"Invoices/Show","props":{"invoiceId":7},"url":"/invoices/7","version":"1"}
There are four pieces here. component names the page component. props is its data. url is the page's address. version identifies the current build of your JavaScript and CSS; Chapter 7 covers what happens when it changes. A page object from this crate carries more than these four, and the chapters introduce each extra field where it is used.
Full definition Page component
- Kind
- A React, Vue or Svelte component.
- Behavior
- Renders one whole page from the props it is given.
- Boundary
- Not a layout or a partial. A page object names exactly one page component.
- Instance
Invoices/Show.- Analogue
- A Blade view file, except it runs in the browser. This comparison is the author's.
Full definition Props
- Kind
- A JSON object, held in the page object's
propsfield. - Behavior
- Carries data from a handler to a page component.
- Boundary
- Not component state. Props come from the server with each visit.
- Instance
{"invoiceId":7}.- Analogue
- The array passed as the second argument to
view()in Laravel. This comparison is the author's.
Full definition Page object
- Kind
- A JSON object.
- Parts
- Four required fields:
component,props,urlandversion. Other fields appear only when they apply. - Behavior
- Tells the client which page component to show and what data to give it.
- Boundary
- Not an API resource. It describes a page, not a record.
- Instance
{"component":"Invoices/Show","props":{"invoiceId":7},"url":"/invoices/7","version":"1"}, trimmed to the required fields.
A browser's first visit to an Inertia application is an ordinary request, and the answer is a full HTML document with the page object inside it, in a <script type="application/json"> element. Every visit after that is made by the Inertia client, which adds the header X-Inertia: true, and the server answers with the page object alone. The client then swaps one page component for another without reloading the document.
What Is an Adapter?
The protocol is not tied to one server framework. Any backend that speaks HTTP can implement it, and the Laravel adapter is the reference implementation. An adapter is the server-side library that does the implementing: it decides whether a request gets a document or JSON, builds the page object, and applies the protocol's rules for redirects and versions.
Full definition Adapter
- Kind
- A server-side library that implements the Inertia protocol.
- Behavior
- Decides whether a request gets an HTML document or JSON, builds the page object, and applies the protocol's rules for redirects and versions.
- Boundary
- Not the Inertia client. The client runs in the browser; the adapter runs on the server.
- Instance
inertia-laravel-rustfor Rust. The Laravel adapter is the reference implementation.
inertia-laravel-rust is an adapter for Rust. Its core depends on no web framework, and a feature named axum connects it to Axum, the web framework the listings in these chapters use. It ships as a crate, the unit of code the Rust compiler compiles at a time; a library you depend on is a crate. Your code imports this one as inertia, whatever the package is called.
Full definition Crate
- Kind
- Rust's compilation unit: the smallest amount of code the compiler considers at a time.
- Parts
- Two forms: a binary crate, which is a program, and a library crate, which other code depends on.
- Behavior
- Compiles as one unit.
- Boundary
- Not a package. A package is a bundle of one or more crates that Cargo builds and shares.
- Instance
inertia-laravel-rustis a library crate that your code imports asinertia.- Analogue
- A Composer package, loosely. This comparison is the author's.
What Is a Struct?
Chapter 1 has the word in its title, so here it is. A struct is a Rust type that groups several named values. Each value is a field, and each field has its own type. Example 0-2 defines a struct and then makes one.
Example 0-2. A struct and an instance of it
struct Invoice {
id: u64,
total_cents: i64,
}
let invoice = Invoice { id: 7, total_cents: 1200 };
There are two pieces here. The first block is the definition: the keyword struct, a name, and the fields with their types. The last line is an instance, with a value for every field. If you write PHP, I'd read a struct as a class with typed public properties and no methods.
Full definition Struct
- Kind
- A Rust type, defined with the keyword
struct. - Parts
- Named fields, each with its own type. The definition fixes how many.
- Behavior
- Groups related values under one name.
- Boundary
- Not a tuple. A tuple's values are told apart by position; a struct's values are named.
- Instance
Invoice { id: 7, total_cents: 1200 }.- Analogue
- A PHP class with typed public properties and no methods. This comparison is the author's.
A struct matters to these chapters because it can become JSON. The crate serde does that conversion, and the line #[derive(Serialize)] above a struct asks serde to generate the code for it. Chapter 1 puts that line on a struct and hands the struct to a page as its props.
One Handler
A handler is the function a route calls. If you write Laravel, I'd read it as a controller method. Example 0-3 is the handler from the crate's quick start.
Example 0-3. A handler that renders a page
async fn home(inertia: Inertia) -> inertia::Response {
inertia.render("Home", props! { "greeting" => "Hello from Rust" })
}
There are three new pieces here. The parameter inertia: Inertia is a handle for the current request, which the Axum adapter supplies. render takes the name of a page component and its props. props! is a macro that builds props from pairs of a key and a value.
Full definition Handler
- Kind
- A function that a route calls.
- Behavior
- Returns the response for one request.
- Boundary
- Not middleware. Middleware runs around a handler; Chapter 2 shares props from one.
- Instance
homein the crate's quick start, which returnsinertia.render("Home", ...).- Analogue
- A Laravel controller method. This comparison is the author's.
As you can see, the handler doesn't ask which kind of visit it is answering. The same three lines produce the HTML document for a first visit and the JSON for every visit after it.
How to Read the Chapters
Each chapter takes one mechanism, says at the top what it covers and what it leaves out, and numbers its listings. Each one ends with a test, a summary, and a list of its sources with the file and lines to look at. All of them were written against one commit of the crate, 3ccc126.
If you're unsure where to start, I'd read Chapter 1 and Chapter 2 in order and take the rest as you need them.
TL;DR
Inertia is a client-side library and a protocol: the server answers a visit with a page object, which names a page component and carries its props. The first visit gets an HTML document with the page object inside it, and later visits, marked by X-Inertia: true, get the page object as JSON. An adapter implements the server side, and inertia-laravel-rust is one for Rust with a connection to Axum. A struct is a Rust type with named, typed fields, and a handler is the function a route calls.
¹ Inertia's own documentation describes the server's routing, controllers and middleware as staying where they are, with JavaScript page components taking the place of the server-side view layer.