# SPA Mode

> Single Page Applications handle navigation client-side without full page reloads. The library has built-in SPA support to re-detect page types on route changes.

## Enabling SPA Mode

Set `spa: true` in your [configuration](/02-configuration):

```typescript
new Initializer({
  // ...required fields
  spa: true,
  pageMap: {
    /* your page map */
  },
});
```

## How It Works

When enabled, the `Page` class:

1. Listens for URL changes via `popstate` events
2. Intercepts `pushState` and `replaceState` calls
3. Re-runs page detection on every navigation
4. Sends a new tracking event for each detected page

You **don't need to manually call** tracking events on route change — the library handles it automatically.

## When to Use

| Scenario                                       | Use `spa: true` |
| ---------------------------------------------- | --------------- |
| React / Vue / Angular with client-side routing | Yes             |
| Next.js with full page reloads (`<a>` tags)    | No              |
| Next.js App Router with `Link` component       | Yes             |
| Traditional multi-page site                    | No              |
| Hybrid (some pages reload, some don't)         | Yes             |

## Manual Alternative

You can skip SPA mode and call events manually in your router:

```typescript
router.afterEach(to => {
  if (to.name === 'product') {
    events.ProductView({ productId: to.params.id });
  } else {
    events.PageView({ category: to.meta.category });
  }
});
```

## Cleanup

When your SPA unmounts (e.g., micro-frontend), call `destroy()` to remove listeners:

```typescript
initializer.destroy();
```

## See Also

- [Configuration](/02-configuration)
- [Page Mapping](/03-page-mapping)
