# @ngrx-addons/persist-state

> Persist state in storage for ngrx, based on @ngneat/elf-persist-state

Latest version **11.0.0** (published 2026-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ngrx-addons/persist-state
pnpm add @ngrx-addons/persist-state
yarn add @ngrx-addons/persist-state
bun add @ngrx-addons/persist-state
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 11.0.0 |
| Published | 2026-09-28 |
| First published | 2022-09-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 2 |
| Unpacked size | 57.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 5 |
| Author | Michał Mrozek |
| Maintainers | michaukrieg |
| Keywords | ngrx, persist state, persist store, storage, local storage, session storage, indexed db |

## Links

- npm: https://www.npmjs.com/package/@ngrx-addons/persist-state
- Repository: https://github.com/Michsior14/ngrx-addons
- Homepage: https://github.com/Michsior14/ngrx-addons#readme
- Issues: https://github.com/Michsior14/ngrx-addons/issues
- npm.io page: https://npm.io/package/@ngrx-addons/persist-state

## Dependencies (2)

- [tslib](https://npm.io/package/tslib.md) ^2.3.0
- [@ngrx-addons/common](https://npm.io/package/@ngrx-addons/common.md) 11.0.0

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 11.0.0 (latest) — 2026-09-28
- 10.0.0 — 2026-08-28
- 9.0.1 — 2026-02-20
- 9.0.0 — 2025-07-30
- 8.0.3 — 2025-06-13
- 8.0.2 — 2025-03-22
- 8.0.1 — 2025-03-01
- 8.0.0 — 2024-12-17
- 7.0.0 — 2024-06-14
- 6.0.0 — 2024-05-29
- 5.0.1 — 2024-01-26
- 5.0.0 — 2023-11-21
- 4.0.1 — 2023-10-31
- 4.0.0 — 2023-08-10
- 3.2.4 — 2023-08-05
- … 16 more at https://npm.io/package/@ngrx-addons/persist-state/versions

## README

# @ngrx-addons/persist-state ![npm bundle size](https://img.shields.io/bundlephobia/minzip/@ngrx-addons/persist-state?color=%236969b3) ![npm](https://img.shields.io/npm/dm/@ngrx-addons/persist-state?color=%2351ae17) ![NPM](https://img.shields.io/npm/l/@ngrx-addons/persist-state?color=%23f55d3e) ![npm](https://img.shields.io/npm/v/@ngrx-addons/persist-state?color=%2366101f)

The library for persisting state in ngrx. Highly inspired by [@ngneat/elf-persist-state](https://github.com/ngneat/elf/tree/master/packages/persist-state).
Supports local storage, session storage and async storages with [localForage](https://github.com/localForage/localForage).

## Supported versions

- `angular` 22+
- `@ngrx/store` 22+

## Installation

```bash
npm i @ngrx-addons/persist-state
```

or

```bash
yarn add @ngrx-addons/persist-state
```

## Usage

The module gives ability to persist some of the app’s states, by saving it to `localStorage/sessionStorage` or anything that implements the `StorageEngine API`, and restore it after a refresh. It supports both root and feature states. The only thing you need to do is to add `PersistStateModule.forRoot`/`providePersistStore` to your `AppModule` and `PersistStateModule.forFeature`/`providePersistState` to your feature module.

### For root states

```ts
import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { BeforeAppInit } from '@ngrx-addons/common';
import { PersistStateModule, localStorageStrategy } from '@ngrx-addons/persist-state';

const counterReducer = ...;
const reducers = {
  counter: counterReducer,
} as const;

@NgModule({
  imports: [
    StoreModule.forRoot(reducers),
    // Define after EffectsModule.forRoot() if you want to listen on `rehydrate` action
    // type provided for hints on states
    PersistStateModule.forRoot<typeof reducers>({
      states: [
        {
          key: 'counter',
          // the package exposes localStorageStrategy and
          // sessionStorageStrategy, optionally you can
          // provide your own implementation or even
          // use localForage for indexed db.
          storage: localStorageStrategy,
          // optional options (default values)
          runGuard: () => typeof window !== 'undefined',
          source: (state) => state,
          storageKey: `${storageKeyPrefix}-${key}@store`,
          migrations: [],
          skip: 1
        },
        // next states to persist, same reducer key can be
        // specified multiple times to save parts of the state
        // to different storages
      ],
      // optional root options (for all, also feature states)
      storageKeyPrefix: 'some-prefix',
      // optional rehydration strategy
      strategy: BeforeAppInit, // or AfterAppInit
    }),
  ],
})
export class AppModule {}
```

or in case of using standalone API:

```ts
import { NgModule } from '@angular/core';
import { provideStore } from '@ngrx/store';
import { BeforeAppInit } from '@ngrx-addons/common';
import { providePersistStore, localStorageStrategy } from '@ngrx-addons/persist-state';

const counterReducer = ...;
const reducers = {
  counter: counterReducer,
} as const;

@NgModule({
  providers: [
    provideStore(reducers),
    // Define after EffectsModule.forRoot() if you want to listen on `rehydrate` action
    // type provided for hints on states
    providePersistStore<typeof reducers>({
      states: [
        {
          key: 'counter',
          // the package exposes localStorageStrategy and
          // sessionStorageStrategy, optionally you can
          // provide your own implementation or even
          // use localForage for indexed db.
          storage: localStorageStrategy,
          // optional options (default values)
          runGuard: () => typeof window !== 'undefined',
          source: (state) => state,
          storageKey: `${storageKeyPrefix}-${key}@store`,
          migrations: [],
          skip: 1
        },
        // next states to persist, same reducer key can be
        // specified multiple times to save parts of the state
        // to different storages
      ],
      // optional root options (for all, also feature states)
      storageKeyPrefix: 'some-prefix',
      // optional rehydration strategy
      strategy: BeforeAppInit, // or AfterAppInit
    }),
  ],
})
export class AppModule {}
```

The `forRoot`/`providePersistStore` method accepts an object with the following properties:

- `states` - array of states configs (defined below, required)
- `storageKeyPrefix` - prefix for all storage keys (optional)
- `strategy` - defines if rehydrate actions should be fired before or after app initialization (optional, default: `BeforeAppInit`). State changes are saved only after the stored state has been restored. Changes made before that (for example during startup with `AfterAppInit`, or while an async storage is loading) are saved when the state next changes, or right after the restore with `skip: 0`.

Each state can be described by multiple state configs with the following properties:

- `key` - the reducer key in app state (required)
- `storage`: an object or function resolving to an object with async setItem, getItem and removeItem methods for storing the state. The package exposes `localStorageStrategy` or `sessionStorageStrategy`, excepts also a `localForage` instance (required).
- `source`: a method that receives the observable of a state and return what to save from it (by default - the entire state).
- `storageKey`: the name under which the store state is saved (by default - the prefix plus store name plus a `@store` suffix).
- `runGuard` - returns whether the actual implementation should be run. The default is `() => typeof window !== 'undefined'`
- `skip` - the number of state changes skipped after the stored state has been restored. Used to avoid saving the state that was just restored. `undefined` values aren't counted. The default is `1`.
- `migrations` - the array of migrations to run on the state before `rehydrated` event is fired. The default is `[]`.
  - `version` - the version of the state to migrate from.
  - `versionKey` - the key in the state that contains the version. The default is `version`.
  - `migrate` - the function that receives the state and returns the migrated state.

### For feature states

Register each feature only once. If the same key is registered more than once, only the first configuration is used, and the state is persisted until every registration is removed.

```ts
import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { PersistStateModule, localStorageStrategy } from '@ngrx-addons/persist-state';

interface CounterState {
  count: number;
}
const counterReducer = ...;

@NgModule({
  imports: [
    StoreModule.forRoot(),
    // forRoot should be always called, similar to ngrx StoreModule and it's forFeature implementation.
    PersistStateModule.forRoot(),
  ],
})
export class AppModule {}

@NgModule({
  imports: [
    StoreModule.forFeature('counter', reducer),
    // type provided for hints on states
    PersistStateModule.forFeature<CounterState>({
      key: 'counter',
      states: [
        {
          // The same options as for root states, except the key
          storage: localStorageStrategy
        },
      ],
    }),
  ]
})
export class CounterModule {}
```

or in case of using standalone API:

```ts
import { NgModule } from '@angular/core';
import { provideStore, provideState } from '@ngrx/store';
import { providePersistStore, providePersistState, localStorageStrategy } from '@ngrx-addons/persist-state';

interface CounterState {
  count: number;
}
const counterReducer = ...;

@NgModule({
  providers: [
    provideStore(),
    // forRoot should be always called, similar to ngrx StoreModule and it's forFeature implementation.
    providePersistStore(),
  ],
})
export class AppModule {}

@NgModule({
  providers: [
    provideState('counter', reducer),
    // type provided for hints on states
    providePersistState<CounterState>({
      key: 'counter',
      states: [
        {
          // The same options as for root states, except the key
          storage: localStorageStrategy
        },
      ],
    }),
  ]
})
export class CounterModule {}
```

The `forFeature`/`providePersistState` method accepts an object with the following properties:

- `key` - the feature key (required)
- `states` - array of states configs as in `forRoot`, except `key` property (required)

Once the state is rehydrated, the action (`rehydrate`, type: `@ngrx-addons/persist-state/rehydrate`) with the proper `features` is dispatched (multiple times). You can use it to react in `effects` or `meta-reducers`.

Errors from storages, `source` and `migrations` are reported to Angular's `ErrorHandler`. A failed save doesn't stop later saves, and a failed restore disables saving for that state only.

## Excluding/Including keys from the state​

The `excludeKeys()`/`includeKeys()` operator can be used to exclude keys from the state:

```ts
import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { excludeKeys, includeKeys } from '@ngrx-addons/common';
import { PersistStateModule, localStorageStrategy } from '@ngrx-addons/persist-state';

const counterReducer = ...;
const reducers = {
  counter: counterReducer,
} as const;

@NgModule({
  imports: [
    StoreModule.forRoot(reducers),
    PersistStateModule.forRoot<typeof reducers>({
      states: [
        {
          key: 'counter',
          storage: localStorageStrategy,
          source: (state) => state.pipe(excludeKeys(['a', 'b'])),
          // source: (state) => state.pipe(includeKeys(['a', 'b'])),
        },
      ],
    }),
  ],
})
export class AppModule {}
```

### Performance Optimization​

By default, the module will update the storage upon each state changes (`distinctUntilChanged` with object equality check is applied). Some applications perform multiple updates in a second, and update the storage on each change can be costly.

For such cases, it's recommended to use the `debounceTime` operator. For example:

```ts
import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { debounceTime } from 'rxjs/operators';
import { PersistStateModule, localStorageStrategy } from '@ngrx-addons/persist-state';

const counterReducer = ...;
const reducers = {
  counter: counterReducer,
} as const;

@NgModule({
  imports: [
    StoreModule.forRoot(reducers),
    PersistStateModule.forRoot<typeof reducers>({
      states: [
        {
          key: 'counter',
          storage: localStorageStrategy,
          source: (state) => state.pipe(debounceTime(1000)),
        },
      ],
    }),
  ],
})
export class AppModule {}
```

## Examples

Check [apps](../../apps)

---
_Source: https://npm.io/package/@ngrx-addons/persist-state · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
