> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Location and Route Reuse

The in-memory history classes that let Angular Router run without a URL bar, and an opt-in strategy that keeps route views alive. `import { LynxRouteReuseStrategy, LynxLocationStrategy, LynxPlatformLocation } from '@blotch/angular-lynx';`

## Usage

[`provideRouter()`](/api/angular-lynx/routing/index.md#providerouter) installs `LynxLocationStrategy` and `LynxPlatformLocation` for you. Angular's `Location` service calls them, so you rarely call them yourself.

`LynxRouteReuseStrategy` is opt-in. Provide it after `provideRouter()`:

```typescript title="src/app/app.config.ts"
import { RouteReuseStrategy } from '@angular/router';
import { LynxRouteReuseStrategy, provideRouter } from '@blotch/angular-lynx';

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter(routes),
    { provide: RouteReuseStrategy, useClass: LynxRouteReuseStrategy },
  ],
};
```

## API

### `LynxRouteReuseStrategy`

```typescript
@Injectable()
class LynxRouteReuseStrategy implements RouteReuseStrategy {
  shouldDetach(route: ActivatedRouteSnapshot): boolean;
  store(
    route: ActivatedRouteSnapshot,
    handle: DetachedRouteHandle | null,
  ): void;
  shouldAttach(route: ActivatedRouteSnapshot): boolean;
  retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle | null;
  shouldReuseRoute(
    future: ActivatedRouteSnapshot,
    curr: ActivatedRouteSnapshot,
  ): boolean;
}
```

Keeps each route's component alive when you navigate away, and reattaches it when you come back. `provideRouter()` doesn't install it.

- **`ngOnDestroy` doesn't run** when you leave a route, and component state survives.
- **Views are stored by route path,** so `/user/1` and `/user/2` share one stored view.
- **Nothing is ever evicted:** every route you visit stays in memory.
- **It doesn't save native elements.** A reattached route rebuilds its elements, as a new one creates them. See [Renderer internals](/guide/renderer-architecture.md#route-views).

| Method               | Returns                                                                                 |
| -------------------- | --------------------------------------------------------------------------------------- |
| `shouldDetach()`     | Always `true`.                                                                          |
| `store()`            | Nothing. Stores the view under the route's path, or forgets it when `handle` is `null`. |
| `shouldAttach()`     | `true` when a view is stored for the route's path.                                      |
| `retrieve()`         | The stored view, or `null`.                                                             |
| `shouldReuseRoute()` | `true` when both snapshots come from the same route config, as Angular's default does.  |

### `LynxLocationStrategy`

```typescript
@Injectable()
class LynxLocationStrategy extends LocationStrategy {
  readonly canGoBack: Signal<boolean>;
  get historyIndex(): number;
  path(): string;
  prepareExternalUrl(internal: string): string;
  getBaseHref(): string;
  getState(): unknown;
  pushState(
    state: unknown,
    title: string,
    url: string,
    queryParams: string,
  ): void;
  replaceState(
    state: unknown,
    title: string,
    url: string,
    queryParams: string,
  ): void;
  back(): void;
  forward(): void;
  historyGo(relativePosition?: number): void;
  onPopState(fn: LocationChangeListener): VoidFunction;
}
```

A `LocationStrategy` that keeps a history stack in memory, starting at `/`. Angular's `Location` service calls it, so you rarely call it yourself.

| Member                 | What it does                                                                     |
| ---------------------- | -------------------------------------------------------------------------------- |
| `canGoBack`            | `true` when there's an entry before the current one, so `back()` goes somewhere. |
| `historyIndex`         | Where the current entry is in the history, from `0` for the first.               |
| `path()`               | Returns the current URL, including its query string.                             |
| `prepareExternalUrl()` | Returns `internal` unchanged.                                                    |
| `getBaseHref()`        | Always returns `'/'`.                                                            |
| `getState()`           | Returns the current entry's state, or `null`.                                    |
| `pushState()`          | Adds a history entry and drops any forward entries.                              |
| `replaceState()`       | Replaces the current history entry.                                              |
| `back()`, `forward()`  | Same as `historyGo(-1)` and `historyGo(1)`.                                      |
| `historyGo()`          | Moves through history and notifies `popstate` listeners.                         |
| `onPopState()`         | Adds a `popstate` listener. Call the returned function to remove it.             |

- **`canGoBack`:** a replaced entry doesn't count. The [Android back button](/api/angular-lynx/routing/index.md#the-android-back-button) goes back while it's `true`.
- **`historyIndex`:** `LynxStackOutlet` keeps each screen's, so its swipe goes back to the entry before the screen's.
- **`historyGo()`:** a move past either end does nothing.

### `LynxPlatformLocation`

```typescript
@Injectable()
class LynxPlatformLocation extends PlatformLocation {
  get href(): string;
  get protocol(): string; // 'lynx:'
  get hostname(): string; // 'app'
  get port(): string; // ''
  get pathname(): string;
  get search(): string;
  get hash(): string;
  getBaseHrefFromDOM(): string;
  getState(): unknown;
  pushState(state: unknown, title: string, url: string): void;
  replaceState(state: unknown, title: string, url: string): void;
  back(): void;
  forward(): void;
  historyGo(relativePosition?: number): void;
  onPopState(fn: LocationChangeListener): VoidFunction;
  onHashChange(fn: LocationChangeListener): VoidFunction;
}
```

A `PlatformLocation` with its own in-memory history, since Lynx has no browser history or `window.location`. Its URLs are the router's paths, with no origin, so `href` puts `lynx://app` in front of them.

| Member                               | What it does                                                                               |
| ------------------------------------ | ------------------------------------------------------------------------------------------ |
| `href`                               | The current URL: `lynx://app` followed by its path, query string and fragment.             |
| `protocol`, `hostname`, `port`       | Fixed values (`'lynx:'`, `'app'`, `''`), because there's no real server.                   |
| `pathname`                           | The current URL's path, or `'/'` when empty.                                               |
| `search`                             | The query string with its `?`, or `''`.                                                    |
| `hash`                               | The fragment with its `#`, or `''`.                                                        |
| `getBaseHrefFromDOM()`               | Always returns `'/'`.                                                                      |
| `getState()`                         | Returns the current entry's state.                                                         |
| `pushState()`                        | Adds an entry, dropping forward entries.                                                   |
| `replaceState()`                     | Replaces the current entry.                                                                |
| `back()`, `forward()`, `historyGo()` | Move through history and notify `popstate` listeners. A move past either end does nothing. |
| `onPopState()`                       | Adds a listener and returns its remover.                                                   |
| `onHashChange()`                     | Does nothing, because hash changes arrive as `popstate`.                                   |

## Related

- [Routing](/api/angular-lynx/routing/index.md): `provideRouter()`, which installs the location classes.
- [Routing and navigation](/guide/routing/index.md#keep-a-screen-alive): when to keep route views alive.
- [Renderer internals](/guide/renderer-architecture.md#route-views): what a reattached route rebuilds.
- [Angular `RouteReuseStrategy`](https://angular.dev/api/router/RouteReuseStrategy)
