# Lazy Loading

Defer expensive operations to improve initial page load. Two approaches: init-based deferred loading and viewport-based lazy loading.

## Init-based deferred loading

Component renders immediately with a placeholder, then fetches data after page load. Best for
above-the-fold content with expensive data. `lace:init` fires the method on mount, and `lace:loading.init`
shows the placeholder while it runs - no manual loading flag needed.

```interlace-demo
CreditBalance
```

## Viewport lazy loading

Component doesn't render until visible in viewport. Best for below-the-fold content that might not be needed.

```interlace-demo lazy=True active=1
RecentActivity
```

## Declaring lazy on the component

Pass `lazy=True` at the tag, or make a component lazy by default by declaring `lazy = True` on the
class (handy for a component that is always heavy, so callers never have to remember the flag). A tag
`lazy=False` still overrides it for a single use.

```python
class RecentActivity(Component):
    lazy = True   # lazy by default, wherever it's mounted

    def mount(self):
        self.events = Event.objects.recent()  # runs only once visible
```

```django
{% interlace "RecentActivity" %}             {# lazy, from the class default #}
{% interlace "RecentActivity" lazy=False %}  {# force eager for this one spot #}
```

## Comparison

| Approach | When It Loads | Best For |
|---|---|---|
| `lace:init="method"` | Immediately after page render | Above-the-fold content, critical data with slow queries |
| `lazy=True` | When scrolled into viewport | Below-the-fold content, optional sections |
