Every prop so far was computed in the handler and sent on every visit. In this chapter we'll look at four ways to change that: props sent only when asked for, props fetched after the first render, props the client keeps, and how a partial reload chooses among them.
This chapter assumes Chapter 5. It does not cover merge props beyond a mention, or live and broadcast props.
Four loading modes
The handler believed every prop was wanted. It loaded the audit trail on each visit and handed it over with the invoices. The component put the audit trail behind a tab. Most visits ended without the tab being opened.
Sometimes a prop is slow to compute and seldom looked at. Thankfully, a prop can be a callback that runs only when the response includes it. Take a look at Example 6-1.
Example 6-1. Four props, four schedules
pub async fn index(inertia: Inertia) -> inertia::Response {
inertia.render(
"Invoices/Index",
props! {
"invoices" => ["INV-7", "INV-8"],
"audit" => inertia::optional(|| async { ["created", "sent"] }),
"totals" => inertia::defer(|| async { 2400 }),
"plans" => inertia::once(|| async { ["basic", "pro"] }),
},
)
}
Let's walk through the four. invoices is a plain value and is sent on every full visit. optional wraps a callback that is sent only when a partial reload asks for it. defer leaves the prop out of the first render and tells the client to fetch it right after. once is resolved once, and the client remembers it on later visits.
Example 6-2 is the first visit.
Example 6-2. The first visit
{"component":"Invoices/Index",
"props":{"errors":{},"app_name":"Ledger","auth":{"user":"Mary"},
"invoices":["INV-7","INV-8"],"plans":["basic","pro"]},
"url":"/invoices","version":"1",
"sharedProps":["errors","app_name","auth"],
"deferredProps":{"default":["totals"]},
"onceProps":{"plans":{"prop":"plans","expiresAt":null}}}
As you can see, audit and totals are absent from props. totals is listed under deferredProps, in a group named default, which is how the client knows to come back for it. plans is present and also listed under onceProps.
Partial reloads
A partial reload is an ordinary Inertia visit with two more headers: one naming the component and one naming the props it wants (see Example 6-3).
Example 6-3. Asking for one prop
let request = visit("GET", "/invoices")
.header("x-inertia-partial-component", "Invoices/Index")
.header("x-inertia-partial-data", "audit")
.body(Body::empty())
.unwrap();
The reply's props hold errors and audit and nothing else. The callbacks for totals and plans did not run, and the shared props app_name and auth were not sent either.
You can also mark a prop always, which sends it with every response, including partial reloads that didn't name it. If you're unsure whether a shared prop needs that, I'd check what your layout reads after a partial reload before adding it.
You can also put deferred props in named groups with .group("name"), so the client fetches the props of a group together.
What this chapter leaves open
So the audit trail is no longer loaded for visits that never look at it. Deferring totals is a different trade: the page arrives sooner and the server answers twice. Is the second request cheaper than the wait it replaced? How would you find out for your slowest prop, and have you?
Testing
Example 6-4 fails if an optional or deferred prop leaks into a first visit.
Example 6-4. What a first visit must not carry
AssertablePage::from_body(&reply.body)
.has_count("invoices", 2)
.missing("audit")
.missing("totals")
.has("plans")
.deferred("default", &["totals"]);
TL;DR
optional props are sent only to a partial reload that names them. defer props are left out of the first render and listed in deferredProps for the client to fetch. once props are sent, listed in onceProps, and skipped when the client says it already has them. A partial reload sends the named props and errors; always overrides that for a prop.