Pattern Resilience Advanced
Async Boundary
An async boundary wraps a group of components and shows one loading state for the whole group. The components inside just render their data, and don’t each have to keep track of their own request.
The problem
A dashboard often loads in pieces. The header appears first, then a spinner where the chart should be, then the sidebar pops in, then another spinner, and then the chart finally shows up while a third section is still loading. The layout keeps jumping around until everything has arrived.
The pattern
An async boundary wraps a group of related components and shows one loading state for the whole group, instead of one for each component.
Problem
If nothing coordinates the loading states, each component handles its own, so one widget might show a skeleton while another is just empty. Users can’t tell what’s loaded and what’s still coming, and the whole page feels janky.
A single spinner for the whole page isn’t much better, if it stays up until even the least important widget has loaded. Users could be reading the main content, but they’re looking at a spinner because one small widget in the corner is slow.
Solution
You’d usually put boundaries around parts of the page that belong together, such as the main content and the sidebar. The boundary shows a single fallback until all of its children are ready, and then they all appear together, so the section doesn’t flicker or fill in piece by piece.
The boundary shows its fallback whenever a child suspends, which is what happens when the child tries to render with data from a promise that hasn’t resolved yet. A React component suspends by passing the promise to use(), and a Vue or Svelte component suspends with a top-level await. Data libraries that support Suspense do the same thing for you.
With more than one boundary on the page, different parts can load at different speeds. You can give the important content its own boundary so it appears first, and give the less important sections their own boundaries so they load independently. Each section still appears all at once.
Example
In each version, one boundary wraps two widgets from the dashboard described in the Problem section. Each widget fetches its own data, and the boundary shows one skeleton until both are ready, so the two widgets appear together. Web components don’t have a built-in boundary, so that version builds a simple one, where each child sends an event when it starts its request.
One Boundary for a Group
import { Suspense, use } from 'react';
// use() needs the same promise on every render, so cache one per URL
const requests = new Map();
function getJSON(url) {
if (!requests.has(url)) requests.set(url, fetch(url).then(r => r.json()));
return requests.get(url);
}
function Dashboard() {
return (
<Suspense fallback={<WidgetsSkeleton />}>
<RevenueChart />
<RecentOrders />
</Suspense>
);
}
function RevenueChart() {
// Suspends here until the promise resolves (React 19+)
const revenue = use(getJSON('/api/revenue'));
return <Chart data={revenue} />;
}
function RecentOrders() {
const orders = use(getJSON('/api/orders'));
return <OrderTable orders={orders} />;
} <!-- Dashboard.vue -->
<template>
<Suspense>
<!-- The default slot takes a single root node -->
<div class="widgets">
<RevenueChart />
<RecentOrders />
</div>
<template #fallback>
<WidgetsSkeleton />
</template>
</Suspense>
</template>
<!-- RevenueChart.vue (RecentOrders.vue fetches /api/orders the same way) -->
<script setup>
// A top-level await makes setup async, so Suspense shows the fallback until it resolves
const revenue = await fetch('/api/revenue').then(r => r.json());
</script>
<template>
<Chart :data="revenue" />
</template> <!-- Dashboard.svelte -->
<svelte:boundary>
<RevenueChart />
<RecentOrders />
{#snippet pending()}
<WidgetsSkeleton />
{/snippet}
</svelte:boundary>
<!-- RevenueChart.svelte (RecentOrders.svelte fetches /api/orders the same way) -->
<script>
// await in a component needs the experimental.async compiler option
const revenue = await fetch('/api/revenue').then(r => r.json());
</script>
<Chart data={revenue} /> class AsyncBoundary extends HTMLElement {
pending = 0;
constructor() {
super();
this.attachShadow({ mode: 'open' }).innerHTML = '<slot></slot>';
// Each child sends its request's promise in an event
this.addEventListener('async-pending', (event) => {
const done = () => { this.pending--; this.update(); };
this.pending++;
this.update();
event.detail.then(done, done);
});
}
update() {
// Show the fallback slot until every announced request has finished
this.shadowRoot.innerHTML = this.pending > 0
? '<slot name="fallback"></slot>'
: '<slot></slot>';
}
}
customElements.define('async-boundary', AsyncBoundary);
class RevenueChart extends HTMLElement {
connectedCallback() {
const request = fetch('/api/revenue')
.then(r => r.json())
.then(revenue => this.render(revenue));
this.dispatchEvent(new CustomEvent('async-pending', { bubbles: true, detail: request }));
}
render(revenue) { /* draw the chart */ }
}
customElements.define('revenue-chart', RevenueChart);
// <async-boundary>
// <widgets-skeleton slot="fallback"></widgets-skeleton>
// <revenue-chart></revenue-chart>
// <recent-orders></recent-orders>
// </async-boundary> A component that fetches in useEffect and stores the result in state doesn’t suspend. It renders right away without its data, so the boundary never shows the fallback for it. A component like that only works with these examples once it fetches through use(), an await, or a library that supports Suspense. Svelte’s {#await} block is a different tool too, because it shows the loading state for one promise inside one component.
Separate Boundaries per Section
The activity feed on the same dashboard is slower and less important, so it gets its own boundary, and the two widgets can appear without it. A boundary like <Suspense> only handles loading. If a request fails, the error goes to the nearest Error Boundary, so each section here has one of those around it as well, and when a request fails, only that section shows an error.
// ErrorBoundary is your own class component, or the one from react-error-boundary
function Dashboard() {
return (
<div className="dashboard">
<ErrorBoundary fallback={<WidgetsError />}>
<Suspense fallback={<WidgetsSkeleton />}>
<RevenueChart />
<RecentOrders />
</Suspense>
</ErrorBoundary>
<ErrorBoundary fallback={<ActivityError />}>
<Suspense fallback={<ActivitySkeleton />}>
<ActivityFeed />
</Suspense>
</ErrorBoundary>
</div>
);
}
Streaming with Server Components
The server streams the HTML for each server component as its data becomes available, so the static content can appear right away.
// Next.js App Router - static content renders immediately, async streams in
export default function Page() {
return (
<div>
<Header />
<Suspense fallback={<ProductGridSkeleton />}>
<ProductGrid />
</Suspense>
<Footer />
</div>
);
}
Delayed Fallback
If you delay the fallback, the spinner doesn’t flash on screen when the content loads quickly. The timer runs inside the fallback component, because React only mounts the fallback when the boundary suspends, so the delay starts at that moment instead of when the page first mounts.
// Avoid spinner flash for fast loads by only showing it after 200ms
function DelayedSpinner({ delay = 200 }) {
const [visible, setVisible] = useState(false);
useEffect(() => {
const timer = setTimeout(() => setVisible(true), delay);
return () => clearTimeout(timer);
}, [delay]);
return visible ? <Spinner /> : null;
}
function PageWithDelayedFallback() {
return (
<Suspense fallback={<DelayedSpinner />}>
<SlowComponent />
</Suspense>
);
}
Benefits
- The UI loads a whole section at a time, so it doesn’t flicker as each piece arrives.
- Each section switches from loading to loaded all at once, so users can tell which parts of the page are ready.
- Components don’t have to manage their own loading state. They just render data, and the boundary shows the fallback while that data loads.
- It’s easy to load the page progressively. You can give secondary content its own boundary so the important content appears first, and with server streaming, the rest fills in as its data arrives.
Tradeoffs
- You need framework support, such as React Suspense, Vue Suspense, or Svelte’s
<svelte:boundary>, which only works withawaitin components when the experimental async option is enabled. Without framework support, you have to build the boundary yourself. - One slow child delays the whole boundary, so if one of four widgets takes five seconds to load, users wait five seconds to see any of them.
- It can be tricky to choose where your boundaries go. With too few, large parts of the page stay hidden until their slowest request finishes, and with too many, there are spinners everywhere.
- A page with many boundaries is harder to debug, because you need to keep track of which boundary depends on which data.
- Components that fetch in
useEffectmight need refactoring before they can suspend. - There’s no built-in timeout, so if a child never resolves, the boundary shows its fallback forever unless you add your own timeout logic.
- Errors need their own error boundary. Without one around each section, a failed request can break everything up to the nearest error boundary, which might be the whole page.
Summary
An async boundary lets you define the loading state for a group of components in one place, so the components themselves only render data. It shows one fallback while the data is fetched, and if you pair it with an error boundary, a failed request only shows an error in that section instead of breaking the whole page.
See also
- Error Boundary — Resilience
- Loading State — Resilience
- Lazy Loading — Performance
- Suspense - React
- Suspense - Vue
- Await Blocks - Svelte
- Streaming and Suspense - Next.js