MARY.WIN

RUSTY — INERTIA — CHAPTER 03

Flash
Data

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

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.

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 — 8
  • flash takes a key and a Serialize value and queues itR1 · src/inertia.rs L125-L136
  • Queued flash data is written to the session when the request endsR1 · src/inertia.rs L25-L27, L189-L216
  • The next page render pulls flash data out of the sessionR1 · src/session/mod.rs L89-L102
  • Flash sits in the page's flash field and is not kept in browser historyR1 and I1 · src/page.rs L37-L39; src/inertia.rs L125-L126; Inertia v3 docs
  • React reads flash data with usePage().flashI1 · Inertia v3 docs, Flash data
  • Response::flash flashes to the page being renderedR1 · src/response.rs L79-L84
  • Without a session, flash data is discarded with a warningR1 · src/inertia.rs L211-L213
  • Flash survives a redirect and lasts one page; session key inertia.flash_data; session layer wraps the Inertia layerR1 · src/inertia.rs L193-L195; src/session/mod.rs L20, L104-L112; examples/quick_start.rs L33-L34

R1: inertiajs/inertia-laravel-rust @3ccc126a24c8fb5bddb140876a1825292c0d216c
I1: Inertia.js v3 documentation, Flash data, read 2026-11-29