Skip to main content

Configuration

Root Configuration

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

PropertyTypeDefaultDescription
statesSyncStateConfig[]Array of state configs (see below)
channelPrefixstring''Prefix for all Broadcast Channel names
strategyInitializationStrategyBeforeAppInitWhen to start syncing (BeforeAppInit or AfterAppInit)

State Configuration

Each entry in the states array accepts:

PropertyTypeDefaultDescription
keystringRequired. The reducer key in app state
channelstring${prefix}${key}@storeThe Broadcast Channel name
source(state$: Observable) => Observable(state) => stateTransform the state before broadcasting
runGuard() => booleansee belowWhether syncing should run
skipnumber1Number of state changes to skip before syncing

Default runGuard

() => typeof window !== 'undefined' && typeof window.BroadcastChannel !== 'undefined';

This prevents syncing from running in SSR environments or browsers without Broadcast Channel support.

Feature Configuration

The forFeature / provideSyncState method accepts:

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

Filtering Synced State

Use excludeKeys() and includeKeys() from @ngrx-addons/common to control which parts of the state are synced:

import { excludeKeys } from '@ngrx-addons/common';

provideSyncStore({
states: [
{
key: 'counter',
source: (state) => state.pipe(excludeKeys(['localOnly'])),
},
],
});

Initialization Strategies

Same as persist-state — see Initialization Strategies.

Server-Side Rendering (SSR)

The default runGuard already handles SSR by checking for both window and BroadcastChannel. No additional configuration is needed.

For testing, provide a custom runGuard:

provideSyncStore({
states: [
{
key: 'counter',
runGuard: () => false,
},
],
});