Skip to main content

Configuration

Root Configuration

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

PropertyTypeDefaultDescription
statesPersistStateConfig[]Array of state configs (see below)
storageKeyPrefixstring''Prefix for all storage keys
strategyInitializationStrategyBeforeAppInitWhen to fire rehydrate actions (BeforeAppInit or AfterAppInit)

State Configuration

Each entry in the states array accepts:

PropertyTypeDefaultDescription
keystringRequired. The reducer key in app state
storageStateStorage | () => StateStorageRequired. Storage backend. The package provides localStorageStrategy, sessionStorageStrategy, and noopStorage. Also accepts a localForage instance.
source(state$: Observable) => Observable(state) => stateTransform the state before saving (e.g. filter keys)
storageKeystring${prefix}${key}@storeThe key under which the state is stored
runGuard() => boolean() => typeof window !== 'undefined'Whether persistence should run. Returns false in SSR.
skipnumber1Number of state changes to skip before persisting (skips the initial state)
migrationsMigration[][]Array of migrations to run before rehydration

Feature Configuration

The forFeature / providePersistState method accepts:

PropertyTypeDescription
keystringRequired. The feature key
statesPersistStateConfig[]Required. Same as root state config, except without key

Storage Strategies

The package exports three built-in storage strategies:

localStorageStrategy

Persists state to window.localStorage. Falls back to noopStorage if localStorage is unavailable.

sessionStorageStrategy

Persists state to window.sessionStorage. Falls back to noopStorage if sessionStorage is unavailable.

noopStorage

A no-op storage that does nothing. Useful for testing or explicitly disabling persistence:

import { noopStorage } from '@ngrx-addons/persist-state';

providePersistStore({
states: [{ key: 'counter', storage: noopStorage }],
});

Custom Storage

Implement the StateStorage interface for custom backends:

import type { StateStorage } from '@ngrx-addons/persist-state';

const customStorage: StateStorage = {
getItem: (key) => /* return Observable or Promise */,
setItem: (key, value) => /* return Observable or Promise */,
removeItem: (key) => /* return Observable or Promise */,
};

You can also use localForage directly — it implements a compatible interface for IndexedDB, WebSQL, and localStorage.

Migrations

Migrations allow you to transform persisted state when your state shape changes between versions:

providePersistStore({
states: [
{
key: 'counter',
storage: localStorageStrategy,
migrations: [
{
version: 1,
versionKey: 'version', // default
migrate: (state) => ({
...state,
newProperty: 'default',
version: 2,
}),
},
],
},
],
});

Each migration specifies:

PropertyTypeDefaultDescription
versionstring | numberThe version to migrate from
versionKeystring'version'Which key in the state holds the version
migrate(state) => stateThe migration function

Migrations run sequentially before the rehydrate action is dispatched. Add a version field to your persisted state to track which migrations have been applied.