A flash message is data for the next page and no page after it. In this chapter we'll flash a typed notice across a redirect, see where it lands on the page, and see what happens to it when there is no session.
This chapter assumes Chapters 1 and 2. It does not cover displaying the notice; that is React code and depends on your toast component.
Flashing across a redirect
The handler flashed notice and redirected, and considered the customer informed. There was no session layer on the router. The crate wrote one warning line to the log and threw the notice away.
Sometimes a handler finishes its work and has something to say on the page it redirects to. Thankfully, the per-request handle can hold that for you. Take a look at Example 3-1.
Example 3-1. A typed notice
#[derive(Serialize, TS)]
#[serde(rename_all = "lowercase")]
pub enum FlashLevel {
Success,
Error,
}
#[derive(Serialize, TS)]
pub struct Flash {
pub level: FlashLevel,
pub body: String,
}
pub async fn send(inertia: Inertia) -> Response {
inertia.flash("notice", Flash { level: FlashLevel::Success, body: "Invoice sent".into() });
inertia::redirect("/invoices/7").into_response()
}
There are two new pieces here. inertia.flash takes a key and any Serialize value and queues it. inertia::redirect sends the browser on. The queued data is written to the session when the request ends, and the next page render takes it out again.¹
The generated types are in Example 3-2.
Example 3-2. The generated types
type FlashLevel = "success" | "error";
type Flash = { level: FlashLevel, body: string, };
On the page object, flash data sits in its own flash field, beside props and not inside it. The crate's documentation gives the reason: flash data isn't kept in the browser history. In React, Inertia's v3 documentation reads it with usePage().flash.
You can also flash to the page you are rendering right now, with Response::flash, when there is no redirect in between.
Flash data needs a session. The router in this book wraps the Inertia layer in a tower-sessions layer, and the order matters: the session layer goes on last so that it is outermost.
What this chapter leaves open
So the notice arrives once and is gone on the next visit. Now consider the same handler behind a load balancer, with the in-memory session store this chapter uses. Which server holds the notice? Which server gets the redirect? What does the customer see when those are different machines?
Testing
Example 3-3 visits three times with one session cookie. It fails if the notice doesn't arrive, and it fails if the notice arrives twice.
Example 3-3. One page, then gone
let reply = send(&app, visit("POST", "/invoices/7/send").body(Body::empty()).unwrap()).await;
let cookie = reply.cookie.expect("a session cookie");
let reply = send(&app, visit("GET", "/invoices/7").header(header::COOKIE, &cookie).body(Body::empty()).unwrap()).await;
AssertablePage::from_body(&reply.body)
.flash("notice.level", "success")
.flash("notice.body", "Invoice sent");
let reply = send(&app, visit("GET", "/invoices/7").header(header::COOKIE, &cookie).body(Body::empty()).unwrap()).await;
AssertablePage::from_body(&reply.body).missing_flash("notice");
TL;DR
Inertia::flash queues a serializable value under a key, writes it to the session when the request ends, and delivers it to the next rendered page in that page's flash field. It survives a redirect and lasts one page. Without a session the data is discarded with a warning.
¹ The session key is inertia.flash_data.