Skip to content

Props that arrive later ​

Not every prop has to be in the first response. The server can mark a prop as deferred (sent right after the page renders), optional (sent only when asked for) or merged across pages (for infinite scroll). These components ask for them and show something in the meantime.

Deferred props ​

gts
<Deferred @data='permissions'>
  <:default as |state|>…{{if state.reloading 'Refreshing'}}</:default>
  <:fallback>Loading…</:fallback>
</Deferred>

@data is a prop name or a list of them. The fallback shows until they arrive.

When visible ​

<WhenVisible> asks for an optional prop when its element scrolls into view:

gts
<WhenVisible @data='comments' @buffer={{200}}>
  <:default>…</:default>
  <:fallback>Loading comments…</:fallback>
</WhenVisible>

On the demo's idea page, "similar ideas" is deferred and the comments load when you scroll to them.

Try it ↗

Infinite scroll ​

gts
<InfiniteScroll @data='posts' class='grid gap-4'>
  {{#each @props.posts.data key='id' as |post|}}…{{/each}}
</InfiniteScroll>

<InfiniteScroll> takes @buffer, @manual, @manualAfter, @reverse, @autoScroll, @onlyNext, @onlyPrevious, @preserveUrl and @itemsElement. It has <:previous>, <:next> and <:loading> blocks, which yield loading, fetch, hasMore, autoMode and manualMode.

On <InfiniteScroll>, ...attributes goes on the element that wraps the items, and @as picks its tag: @as="ul" when the items are <li>. On <WhenVisible>, @as is the tag of the empty element it watches, placed before the content (a <li> inside a list, say); the content isn't wrapped.

The demo's Ideas page loads 12 ideas at a time as you scroll. See its source, or

try it ↗ .

Browser-only content ​

Content that only makes sense in the browser (a chart, anything that reads window) goes in <WhenMounted>. Server rendering shows its fallback:

gts
<WhenMounted>
  <:default><Chart @data={{@props.sales}} /></:default>
  <:fallback><Skeleton /></:fallback>
</WhenMounted>

On the server ​

With Laravel Rust, inertia::defer, inertia::optional, inertia::lazy and inertia::scroll_with make these props. The demo's idea handler:

rust
/// `similar` is deferred: the page renders first and asks for it right after. `comments` is optional:
/// only sent when the page scrolls down to them. Both wait a little so the loading states show.
/// `idea` is live: when someone else votes, every open copy of the page reloads it.
pub async fn show(idea: Idea) -> laravel::Result<Page> {
    head::merge(Head::new().title(&idea.title));

    let idea_id = idea.id;
    let category = idea.category.clone();
    let comments_count = idea.comments().count().await?;

    Ok(inertia::render(
        "Ideas/Show",
        props! {
            idea => inertia::lazy(async move || Idea::find(idea_id).await)
                .live(inertia::on_channel::<IdeaVoted>(Channel::public(format!("ideas.{idea_id}"))?)),
            commentsCount => comments_count,
            similar => inertia::defer(async move || {
                task::sleep(1200.milliseconds()).await;

                Idea::query()
                    .filter(Idea::category.eq(&category) & Idea::id.ne(idea_id))
                    .order_by(Idea::votes.desc())
                    .limit(3)
                    .get()
                    .await
            }),
            comments => inertia::optional(async move || {
                task::sleep(700.milliseconds()).await;

                Comment::query()
                    .filter(Comment::idea_id.eq(idea_id))
                    .order_by(Comment::id.desc())
                    .get()
                    .await
            }),
        },
    ))
}

Released under the MIT License.