# Bundle Splitting & Lazy Loading

- **Section:** Core Concepts
- **URL:** https://refast.fastapicloud.dev/docs/concepts/bundle-splitting
- **Markdown:** https://refast.fastapicloud.dev/docs/concepts/bundle-splitting.md
- **Source:** https://github.com/idling-mind/refast-docs/blob/main/docs/pages/concepts/bundle_splitting.py
- **Applies to:** refast 0.12.2

Refast's frontend is split into a small always-loaded **core bundle** and a set of
optional **feature chunks**. By default **no** feature chunks are preloaded — they
load on demand the first time a component from that chunk is rendered. The
`preloaded_features` parameter on `RefastApp` controls which chunks are eagerly
sent to the browser on the initial page load.

## Why it matters

Heavy components like recharts (~180 kB gzipped) or the full Lucide icon set
(~120 kB) add real cost to your initial page load. If a page doesn't use charts
there's no reason to make users download that code.

## The preloaded_features parameter

```python
from refast import RefastApp

# Default — no feature chunks preloaded; all load on demand
app = RefastApp(title="My App")

# Charts only — the recharts chunk is preloaded at startup
app = RefastApp(title="My App", preloaded_features=["charts"])

# All features preloaded — biggest initial bundle, no on-demand loading
app = RefastApp(title="My App", preloaded_features=["charts", "markdown", "katex", "mermaid", "icons", "navigation", "overlay", "controls"])

# Explicitly empty — same as the default, confirms no chunks are preloaded
app = RefastApp(title="My App", preloaded_features=[])

# Core-only client mode — ultra-minimal initial payload (excludes pre-registered component registries)
app = RefastApp(title="My App", client_mode="core")
```

Chunks **not** listed in `preloaded_features` are still downloaded the first time
a component from that chunk is rendered — the difference is *when* the download
happens (initial load vs. on demand).

## Client Mode (`client_mode`)

Refast supports two client loading modes via `client_mode`:

- `"full"` (default) — Loads the standard Refast client bundle with full pre-registered component support.
- `"core"` — Loads an optimized, lightweight core client bundle designed for custom asset deployments and headless core environments.

## Always-loaded core components

The following are **always** available regardless of `preloaded_features`:

- Layout: `Container`, `Row`, `Column`, `Grid`, `Flex`, `Separator`
- Typography: `Heading`, `Paragraph`, `Text`, `Code`, `Link`
- Inputs: `Button`, `Input`, `Textarea`, `Select`, `Checkbox`, `Switch`
- Feedback: `Alert`, `Badge`, `Progress`, `Spinner`, `Toast`
- Display: `Card` family, `Table` primitives, `Tabs`, `Accordion`, `Avatar`, `Image`
- Navigation: `Breadcrumb`, `Sidebar` family, `Pagination`

## Feature chunks

The table below lists every available feature name and what it contains.

### Available Feature Chunks

Sizes are approximate gzipped values. Actual sizes depend on the build version.

Feature name

What it includes

Chunk file

Approx. size

charts

recharts + all chart wrappers

refast-charts-*.js

~180 kB

navigation

Menubar, NavigationMenu, Command palette

refast-navigation-*.js

~45 kB

overlay

Dialog, Sheet, Drawer, Popover, HoverCard

refast-overlay-*.js

~55 kB

controls

Slider, DatePicker, Combobox, ToggleGroup, Carousel

refast-controls-*.js

~70 kB

markdown

Markdown renderer (remark/rehype + syntax highlighting)

refast-markdown-*.js

~95 kB

katex

KaTeX math rendering (remark-math, rehype-katex)

refast-katex-*.js

~50 kB

mermaid

Mermaid diagramming library

refast-mermaid-*.js

~140 kB

icons

Full Lucide icon set (thousands of icons)

refast-icons-*.js

~120 kB

## Choosing the right configuration

| App type | Recommended `preloaded_features` |
|---|---|
| Dashboard with charts | `["charts"]` |
| Admin panel with rich forms | `["controls", "overlay"]` |
| Documentation or content site | `["markdown", "katex", "mermaid", "icons", "navigation"]` |
| Minimal UI / internal tool | `[]` or `None` (default — on demand) |
| Full-featured app | `["charts", "markdown", "katex", "mermaid", "icons", "navigation", "overlay", "controls"]` |

## Example

```python
from refast import RefastApp

# A documentation site — needs markdown rendering, icons, and navigation menus
ui = RefastApp(
    title="My Docs",
    preloaded_features=["markdown", "icons", "navigation"],
)
```

## Measuring the impact

Use your browser's DevTools **Network** tab filtered to **JS** requests. With the default
configuration you will see several `refast-*-*.js` files; with `preloaded_features=[]`
only `refast-client.js` loads on the first request.

```bash
# Run three instances side-by-side to compare (see examples/lazy_loading/)
uvicorn examples.lazy_loading.app:app_all     --port 8001
uvicorn examples.lazy_loading.app:app_charts  --port 8002
uvicorn examples.lazy_loading.app:app_minimal --port 8003
```

## Important notes

- Setting `preloaded_features=[]` does **not** disable any component — it only changes
  when the browser fetches its code (on demand rather than upfront).
- Components from un-preloaded chunks may show a brief loading flash on first render
  while the chunk is being fetched. This is usually imperceptible on a local network
  but can be noticeable over slow connections.
- Use browser caching headers to ensure chunks are only downloaded once per deployment.

## See Also

- [Architecture](https://refast.fastapicloud.dev/docs/architecture) — How the frontend client works
- [Installation](https://refast.fastapicloud.dev/docs/getting-started) — Setting up a Refast app

---

Part of the Refast documentation: [all pages](https://refast.fastapicloud.dev/llms.txt), [everything on one page](https://refast.fastapicloud.dev/llms-full.txt), [search](https://refast.fastapicloud.dev/api/search?q=save_prop).
