Agent Wranglers

APP-57Recommended

Load heavy parts when they are needed

Load each route, and each widget over 20 kB compressed, such as a chart, an editor or a map, only when someone opens it.

How to do it

Split routes with dynamic import(), such as React.lazy, defineAsyncComponent in Vue or @defer in Angular. Load a widget when it scrolls into view or when its tab opens. Reserve its space with a placeholder of the same size. Before adding a dependency, check its compressed size, and prefer the platform, such as Intl instead of a date library. Load the split parts without blocking, as never let a script block the page says.

Examples

TypeScript example

const ReportsScreen = lazy(() => import('./reports/ReportsScreen'));
const RevenueChart = lazy(() => import('./charts/RevenueChart'));

<Suspense fallback={<ChartPlaceholder height={320} />}>
  <RevenueChart accountId={accountId} />
</Suspense>

Python example

@app.get("/panels/revenue-chart")
def revenue_chart():
    series = revenue.monthly(current_user().account_id)
    return render_template("panels/_revenue_chart.html", series=series)

HTML example

<div class="chart-slot" hx-get="/panels/revenue-chart" hx-trigger="revealed" hx-swap="outerHTML"></div>

How to check

Open the first screen with the network log on. No file from a route or widget that is not on screen is requested.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/app-ui/tests-and-budgets/split-heavy-code">Agent Wranglers web app interface playbook</a>. In code you write, add a comment next to it with the same link.