Gathering detailed insights and metrics for persist-and-sync
Gathering detailed insights and metrics for persist-and-sync
Gathering detailed insights and metrics for persist-and-sync
Gathering detailed insights and metrics for persist-and-sync
Zustand middleware to easily persist and sync Zustand state between tabs / windows / iframes (Same Origin)
npm install persist-and-sync
Typescript
Module System
Node Version
NPM Version
94.5
Supply Chain
100
Quality
77.7
Maintenance
100
Vulnerability
100
License
TypeScript (76.78%)
JavaScript (23.22%)
Built with Next.js • Fully responsive • SEO optimized • Open source ready
Total Downloads
238,197
Last Day
228
Last Week
9,846
Last Month
48,298
Last Year
208,589
MIT License
43 Stars
191 Commits
8 Forks
1 Watchers
5 Branches
1 Contributors
Updated on Sep 05, 2025
Latest Version
1.2.3
Package Id
persist-and-sync@1.2.3
Unpacked Size
14.36 kB
Size
5.06 kB
File Count
4
NPM Version
10.8.2
Node Version
20.19.1
Published on
May 23, 2025
Cumulative downloads
Total Downloads
Last Day
-77.8%
228
Compared to previous day
Last Week
-39.3%
9,846
Compared to previous week
Last Month
57.2%
48,298
Compared to previous month
Last Year
604.5%
208,589
Compared to previous year
1
Zustand middleware to easily persist and sync Zustand state between tabs/windows/iframes (Same Origin)
Motivation: Recently I got caught up in several issues working with the persist middleware and syncing tabs with Zustand. This is a simple lightweight middleware to persist and instantly share state between tabs or windows
persist-and-sync
and which to ignore__persistNSyncOptions
in your store.1$ pnpm add persist-and-sync
or
1$ npm install persist-and-sync
or
1$ yarn add persist-and-sync
Add the middleware while creating the store and the rest will be taken care.
1import { create } from "zustand"; 2import { persistNSync } from "persist-and-sync"; 3 4type MyStore = { 5 count: number; 6 set: (n: number) => void; 7}; 8 9const useStore = create<MyStore>( 10 persistNSync( 11 set => ({ 12 count: 0, 13 set: n => set({ count: n }), 14 }), 15 { name: "my-example" }, 16 ), 17);
⚡🎉Boom! Just a couple of lines and your state perfectly syncs between tabs/windows and it is also persisted using localStorage
!
In several cases, you might want to exclude several fields from syncing. To support this scenario, we provide a mechanism to exclude fields based on a list of fields or regular expressions.
1type PersistNSyncOptionsType = { 2 name: string; 3 /** @deprecated */ 4 regExpToIgnore?: RegExp; 5 include?: (string | RegExp)[]; 6 exclude?: (string | RegExp)[]; 7 storage?: "localStorage" | "sessionStorage" | "cookies" /** Added in v1.1.0 */; 8};
Example
1export const useMyStore = create<MyStoreType>()( 2 persistNSync( 3 set => ({ 4 count: 0, 5 _count: 0 /** skipped as it is included in exclude array */, 6 setCount: count => { 7 set(state => ({ ...state, count })); 8 }, 9 set_Count: _count => { 10 set(state => ({ ...state, _count })); 11 }, 12 }), 13 { name: "example", exclude: ["_count"] }, 14 ), 15);
It is good to note here that each element of
include
andexclude
array can either be a string or a regular expression. To use regular expression, you should either usenew RegExp()
or/your-expression/
syntax. Double or single quoted strings are not treated as regular expression. You can specify whether to use either"localStorage"
,"sessionStorage"
, or"cookies"
to persist the state - default"localStorage"
. Please note that"sessionStorage"
is not persisted. Hence can be used for sync only scenarios.
Since version 1.2, you can also update the options at runTime by setting __persistNSyncOptions
in your Zustand state.
Example
1interface StoreWithOptions { 2 count: number; 3 _count: number; 4 __persistNSyncOptions: PersistNSyncOptionsType; 5 setCount: (c: number) => void; 6 set_Count: (c: number) => void; 7 setOptions: (__persistNSyncOptions: PersistNSyncOptionsType) => void; 8} 9 10const defaultOptions = { name: "example", include: [/count/], exclude: [/^_/] }; 11 12export const useStoreWithOptions = create<StoreWithOptions>( 13 persistNSync( 14 set => ({ 15 count: 0, 16 _count: 0 /** skipped as it matches the regexp provided */, 17 __persistNSyncOptions: defaultOptions, 18 setCount: count => set(state => ({ ...state, count })), 19 set_Count: _count => set(state => ({ ...state, _count })), 20 setOptions: __persistNSyncOptions => set(state => ({ ...state, __persistNSyncOptions })), 21 }), 22 defaultOptions, 23 ), 24);
Starting from version 1.2, you can also clear the persisted data by calling clearStorage
function. It takes name
of your store (name
passed in options
while creating the store), and optional storageType
parameters.
1import { clearStorage } from "persist-and-sync"; 2 3... 4 clearStorage("my-store", "cookies"); 5...
In several cases, you might want to exclude several fields from syncing. To support this scenario, we provide a mechanism to exclude fields based on regExp. Just pass regExpToIgnore
(optional - default -> undefined) in the options object.
1// to ignore fields containing a slug 2persistNSync( 3 set => ({ 4 count: 0, 5 slugSomeState: 1, 6 slugSomeState2: 1, 7 set: n => set({ count: n }), 8 }), 9 { name: "my-channel", regExpToIgnore: /slug/ }, 10 // or regExpToIgnore: new RegExp('slug') 11 // Use full power of regExp by adding `i` and `g` flags 12 ),
For more details about regExp check out - JS RegExp
For exactly matching a parameter/field use /^your-field-name$/
. ^
forces match from the first character and similarly, $
forces match until the last character.
use regExpToIgnore: /^(field1|field2|field3)$/
Want a hands-on course for getting started with Turborepo? Check out React and Next.js with TypeScript and The Game of Chess with Next.js, React and TypeScrypt
Licensed as MIT open source.
with 💖 by Mayank Kumar Chaudhari
No vulnerabilities found.
react-persist-local-storage
A React hook for persisting, syncing and managing state in local storage.
mobx-persist-sync
create and persist mobx stores
fullstack-simple-persist
> Tiny self-hosted framework-agnostic state store with extremely simple to use Express and React adapters. Realtime sync, last‑write‑wins, no CRUD boilerplate.
@cullenbond/use-local-storage-sync
React hook to save state to local storage and sync when updates are made to local storage