Paint the user, then the notes

Wrap a slow notes query in inertia::defer and read it on the client with Deferred.

The index loads twenty notes before the first byte of the page. A profile that also loads every note the person has written makes the name wait on that query. inertia::defer omits the prop from the first response and fetches it after the page mounts.

use laravel::inertia::{self, Page, props};
use laravel::prelude::*;

use crate::models::{Note, User};

pub async fn show(user: User) -> Page {
    let user_id = user.id;

    inertia::render("Users/Show", props! {
        user,
        notes => inertia::defer(async move || {
            Note::query()
                .filter(Note::user_id.eq(user_id))
                .latest(Note::created_at)
                .get()
                .await
        }),
    })
}

The callback runs after the handler returns, so it cannot borrow the handler’s locals. async move takes ownership of user_id. The Inertia guide copies the id for the same reason before it queries posts. Do not capture &user and expect it to live.

Register GET /users/{user} on a web route. You already bound User in the HTTP course. This handler replaces the string response from that lesson.

import { Deferred } from "@inertiajs/react";

type User = { id: number; name: string };
type Note = { id: number; title: string };

export default function Show({ user, notes }: { user: User; notes?: Note[] }) {
    return (
        <>
            <h1>{user.name}</h1>
            <Deferred data="notes" fallback={<p>Loading notes…</p>}>
                <ul>
                    {notes?.map((note) => (
                        <li key={note.id}>{note.title}</li>
                    ))}
                </ul>
            </Deferred>
        </>
    );
}

data="notes" matches the prop key. Deferred props on a page are fetched together unless you assign a group with .group(...). .rescue() keeps one failing prop from blocking the others. Both are in the deferred-props section. Field Notes does not need a second group yet.

inertia::optional is the other timing the guide names: the prop is sent only when a partial reload asks for it, such as router.reload({ only: ['notes'] }). Use that when a filter should refresh the list without deferring the first paint. Defer is the right wrapper when the first paint should not wait at all.

task::background is not a substitute. The tasks guide uses it for work that outlives the response inside the process. A restart drops it, and the browser never receives a second prop. A queue job is the substitute when the work must finish even if nobody is looking at the page. That is the next course.

Check your work

Open the profile. The user’s name should appear before the note list, with the fallback visible in between.

Back to roadmap