An Inertia client follows redirects with fetch, and fetch does not behave like a browser's address bar. In this chapter we'll look at the adjustments the crate makes to responses on your behalf, and the one setting that makes the first of them possible: the asset version.
This chapter assumes Chapter 3. It does not cover prefetching, and it mentions history encryption without exercising it.
The stale tab
The browser tab had been open since Tuesday and held Tuesday's JavaScript. On Thursday it asked for the next page as if nothing had happened. The server compared two version strings, found its own 1 against the tab's 0, and answered 409.
Sometimes you deploy new assets while people have the old ones loaded. Thankfully, the version check handles that before your handler runs. Take a look at Example 7-1.
Example 7-1. Setting the version
Config::new().version("1")
There is one piece here. Every Inertia GET visit carries the client's version in a header. When it differs from the server's, the crate replies 409 Conflict with an X-Inertia-Location header holding the full URL, and the client makes a full page load to pick up the new assets. Only GET visits are checked.
You can also compute the version on every request with Config::version_with, for example from a hash of the build manifest.
What happens to your redirects
Example 7-2 is four handlers that each return something ordinary.
Example 7-2. Four ordinary responses
pub async fn archive() -> Response {
inertia::redirect("/invoices").into_response()
}
pub async fn jump() -> Response {
inertia::redirect("/invoices#latest").into_response()
}
pub async fn billing_portal(inertia: Inertia) -> Response {
inertia.location("https://billing.example.com/portal").into_response()
}
pub async fn touch() -> StatusCode {
StatusCode::OK
}
Here is what an Inertia client receives from each.
| Handler | Request | Reply |
|---|---|---|
archive |
PUT |
303 See Other, Location: /invoices |
jump |
POST |
409 Conflict, X-Inertia-Redirect: /invoices#latest |
billing_portal |
GET |
409 Conflict, X-Inertia-Location: https://billing.example.com/portal |
touch |
PUT, with a Referer |
303 See Other, Location set to the Referer |
inertia::redirect produces a 302. After a PUT, PATCH, DELETE or QUERY visit the crate turns that into a 303, so the browser follows it with a GET. A redirect whose URL has a fragment becomes a 409, because fetch drops fragments when it follows a redirect. inertia.location is for a URL the client should load in full, such as another site. An empty 200 becomes a redirect back.
For a request that is not an Inertia visit, none of this applies, and the response only gains a Vary: X-Inertia header.
You can also encrypt a page in the browser's history with encrypt_history, on the configuration or per request, and clear the history on the next page with clear_history. This chapter did not run either.
What this chapter leaves open
So a stale tab is caught, provided the version string changes when the assets do. The example sets it to "1". Who changes it to "2"? Is that a step in your deploy, or a line someone edits? What does a customer see on the day it is forgotten?
Testing
Example 7-3 sends a visit with an old version and fails unless the reply is a 409 with the full URL.
Example 7-3. An outdated client
let request = http::Request::builder()
.uri("/invoices")
.header("host", "localhost")
.header("x-inertia", "true")
.header("x-inertia-version", "0")
.body(Body::empty())
.unwrap();
let reply = send(&app(), request).await;
assert_eq!(reply.status, StatusCode::CONFLICT);
assert_eq!(reply.header("x-inertia-location"), Some("http://localhost/invoices"));
TL;DR
The asset version is compared on every Inertia GET visit, and a mismatch gets a 409 that makes the client reload in full. Redirects after PUT, PATCH, DELETE and QUERY become 303. Redirects with a fragment and calls to location become 409 with a header the client acts on. An empty 200 becomes a redirect back.