<Deferred>
Shows a fallback until the deferred props it names arrive, then its content. The server marks those props as deferred, and Inertia asks for them on its own right after the page renders; <Deferred> only waits for them.
import type { TOC } from '@ember/component/template-only';
import { Deferred, type PageSignature } from 'inertia-ember';
export default <template>
<Deferred @data='stats'>
<:default>{{@props.stats.visits}} visits</:default>
<:fallback>Loading…</:fallback>
</Deferred>
</template> satisfies TOC<PageSignature<{ stats?: { visits: number } }>>;Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
@data | string | string[] | required | The prop, or props, to wait for. Dot paths such as 'user.permissions' work. The content shows once all of them are defined. |
Blocks
| Block | Yields | Shows |
|---|---|---|
<:default> | { reloading: boolean } | Once every prop in @data is defined, and none of them failed. |
<:fallback> | nothing | Until then. Also when a prop failed and there's no <:rescue>. |
<:rescue> | { reloading: boolean } | When the server reports one of the props as rescued (listed in the page's rescuedProps: it failed to load). |
reloading is true while a partial reload that asks for one of these props is in flight: a visit to the same URL, with preserveState: true and an only or except that covers them, such as router.reload({ only: ['stats'] }). A full reload doesn't set it. Before the props first arrive, <:fallback> shows instead.
Element and attributes
None: <Deferred> renders only its blocks, with no wrapping element.
Examples
Several props at once
The server sends user right away and defers permissions and teams. The content shows when both have arrived:
import type { TOC } from '@ember/component/template-only';
import { array } from '@ember/helper';
import { Deferred, type PageSignature } from 'inertia-ember';
interface Props {
user: { name: string };
permissions?: string[];
teams?: { id: number; name: string }[];
}
export default <template>
<h1>{{@props.user.name}}</h1>
<Deferred @data={{array 'permissions' 'teams'}}>
<:default>
<p>{{@props.permissions.length}} permissions</p>
<ul>
{{#each @props.teams key='id' as |team|}}<li>{{team.name}}</li>{{/each}}
</ul>
</:default>
<:fallback><p>Loading permissions and teams…</p></:fallback>
</Deferred>
</template> satisfies TOC<PageSignature<Props>>;Type deferred props as optional: they're undefined until they arrive.
Refreshing without hiding the content
Once the prop is there, a partial reload keeps the content on screen and sets reloading:
import Component from '@glimmer/component';
import { on } from '@ember/modifier';
import { Deferred, router, type PageSignature } from 'inertia-ember';
export default class Dashboard extends Component<PageSignature<{ stats?: { visits: number } }>> {
refresh = () => router.reload({ only: ['stats'] });
<template>
<Deferred @data='stats'>
<:default as |state|>
<p class={{if state.reloading 'faded'}}>{{@props.stats.visits}} visits</p>
<button type='button' disabled={{state.reloading}} {{on 'click' this.refresh}}>Refresh</button>
</:default>
<:fallback><p>Loading stats…</p></:fallback>
</Deferred>
</template>
}When the prop fails
If the server rescues a deferred prop that failed instead of failing the whole response, <:rescue> shows. Reloading the prop clears the failure when it succeeds:
import Component from '@glimmer/component';
import { on } from '@ember/modifier';
import { Deferred, router, type PageSignature } from 'inertia-ember';
export default class Report extends Component<PageSignature<{ report?: { total: number } }>> {
retry = () => router.reload({ only: ['report'] });
<template>
<Deferred @data='report'>
<:default>Total: {{@props.report.total}}</:default>
<:fallback>Building the report…</:fallback>
<:rescue as |state|>
<p>The report couldn't be built.</p>
<button type='button' disabled={{state.reloading}} {{on 'click' this.retry}}>Try again</button>
</:rescue>
</Deferred>
</template>
}Notes
- During server rendering deferred props aren't in the page yet, so the HTML has the fallback.
<Deferred>doesn't request anything itself. To load a prop only when it's needed, use<WhenVisible>with an optional prop, orrouter.reload({ only: [...] }).
Related: Props that arrive later, <WhenVisible>, router, and Inertia's deferred props docs.