{"id":14987548,"url":"https://github.com/react18-tools/persist-and-sync","last_synced_at":"2025-04-07T06:04:42.823Z","repository":{"id":190921950,"uuid":"683585320","full_name":"react18-tools/persist-and-sync","owner":"react18-tools","description":"Zustand middleware to easily persist and sync Zustand state between tabs / windows / iframes (Same Origin)","archived":false,"fork":false,"pushed_at":"2025-02-12T06:14:31.000Z","size":73,"stargazers_count":36,"open_issues_count":2,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T05:02:23.103Z","etag":null,"topics":["javascript","mayank1513","middleware","persist-and-sync","persistent-storage","react","react-library","react18","react18-tools","typescript","typescript-library","typescript-react","zustand"],"latest_commit_sha":null,"homepage":"https://persist-and-sync.vercel.app","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/react18-tools.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["mayank1513"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":["https://pages.razorpay.com/mayank1513"]}},"created_at":"2023-08-27T03:45:36.000Z","updated_at":"2025-03-28T08:18:19.000Z","dependencies_parsed_at":"2023-08-27T05:51:27.894Z","dependency_job_id":"9b5bf506-b836-479c-a997-2a885b6faab5","html_url":"https://github.com/react18-tools/persist-and-sync","commit_stats":{"total_commits":60,"total_committers":1,"mean_commits":60.0,"dds":0.0,"last_synced_commit":"f96582a3ab57199da74e4cdfd2cc6f77439f14eb"},"previous_names":["mayank1513/persist-and-sync"],"tags_count":5,"template":false,"template_full_name":"react18-tools/turborepo-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react18-tools%2Fpersist-and-sync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react18-tools%2Fpersist-and-sync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react18-tools%2Fpersist-and-sync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react18-tools%2Fpersist-and-sync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/react18-tools","download_url":"https://codeload.github.com/react18-tools/persist-and-sync/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601447,"owners_count":20964864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","mayank1513","middleware","persist-and-sync","persistent-storage","react","react-library","react18","react18-tools","typescript","typescript-library","typescript-react","zustand"],"created_at":"2024-09-24T14:14:55.090Z","updated_at":"2025-04-07T06:04:42.800Z","avatar_url":"https://github.com/react18-tools.png","language":"TypeScript","funding_links":["https://github.com/sponsors/mayank1513","https://pages.razorpay.com/mayank1513"],"categories":[],"sub_categories":[],"readme":"# Persist-And-Sync Zustand Store\n\n[![test](https://github.com/react18-tools/persist-and-sync/actions/workflows/test.yml/badge.svg)](https://github.com/react18-tools/persist-and-sync/actions/workflows/test.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/5355eb02cfedc9184e3f/maintainability)](https://codeclimate.com/github/mayank1513/persist-and-sync/maintainability) [![codecov](https://codecov.io/gh/mayank1513/persist-and-sync/graph/badge.svg)](https://codecov.io/gh/mayank1513/persist-and-sync) [![Version](https://img.shields.io/npm/v/persist-and-sync.svg?colorB=green)](https://www.npmjs.com/package/persist-and-sync) [![Downloads](https://img.jsdelivr.com/img.shields.io/npm/d18m/persist-and-sync.svg)](https://www.npmjs.com/package/persist-and-sync) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/persist-and-sync)\n\n\u003e Zustand middleware to easily persist and sync Zustand state between tabs/windows/iframes (Same Origin)\n\n\u003e 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\n\n- ✅ 🐙 ~ 1 kB size cross-tab state sharing + persistence for zustand\n- ✅ Full TypeScript Support\n- ✅ solid reliability in 1 writing and n reading tab scenarios (with changing writing tab)\n- ✅ Fire and forget approach of always using the latest state. Perfect for single-user systems\n- ✅ Share state between multiple browsing contexts\n- ✅ Additional control over which fields to `persist-and-sync` and which to ignore\n- ✅ Optimized for performance using memoization and closures.\n- ✅ Update options at runtime by setting `__persistNSyncOptions` in your store.\n\n## Install\n\n```bash\n$ pnpm add persist-and-sync\n```\n\n**or**\n\n```bash\n$ npm install persist-and-sync\n```\n\n**or**\n\n```bash\n$ yarn add persist-and-sync\n```\n\n## Usage\n\nAdd the middleware while creating the store and the rest will be taken care.\n\n```ts\nimport { create } from \"zustand\";\nimport { persistNSync } from \"persist-and-sync\";\n\ntype MyStore = {\n\tcount: number;\n\tset: (n: number) =\u003e void;\n};\n\nconst useStore = create\u003cMyStore\u003e(\n\tpersistNSync(\n\t\tset =\u003e ({\n\t\t\tcount: 0,\n\t\t\tset: n =\u003e set({ count: n }),\n\t\t}),\n\t\t{ name: \"my-example\" },\n\t),\n);\n```\n\n⚡🎉Boom! Just a couple of lines and your state perfectly syncs between tabs/windows and it is also persisted using `localStorage`!\n\n## Advanced Usage (Customizations)\n\n### PersistNSyncOptions\n\nIn 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.\n\n```typescript\ntype PersistNSyncOptionsType = {\n\tname: string;\n\t/** @deprecated */\n\tregExpToIgnore?: RegExp;\n\tinclude?: (string | RegExp)[];\n\texclude?: (string | RegExp)[];\n\tstorage?: \"localStorage\" | \"sessionStorage\" | \"cookies\" /** Added in v1.1.0 */;\n};\n```\n\n**Example**\n\n```typescript\nexport const useMyStore = create\u003cMyStoreType\u003e()(\n\tpersistNSync(\n\t\tset =\u003e ({\n\t\t\tcount: 0,\n\t\t\t_count: 0 /** skipped as it is included in exclude array */,\n\t\t\tsetCount: count =\u003e {\n\t\t\t\tset(state =\u003e ({ ...state, count }));\n\t\t\t},\n\t\t\tset_Count: _count =\u003e {\n\t\t\t\tset(state =\u003e ({ ...state, _count }));\n\t\t\t},\n\t\t}),\n\t\t{ name: \"example\", exclude: [\"_count\"] },\n\t),\n);\n```\n\n\u003e It is good to note here that each element of `include` and `exclude` array can either be a string or a regular expression.\n\u003e To use regular expression, you should either use `new RegExp()` or `/your-expression/` syntax. Double or single quoted strings are not treated as regular expression.\n\u003e 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.\n\n### Updating options at runtime\n\nSince version 1.2, you can also update the options at runTime by setting `__persistNSyncOptions` in your Zustand state.\n\n**Example**\n\n```ts\ninterface StoreWithOptions {\n\tcount: number;\n\t_count: number;\n\t__persistNSyncOptions: PersistNSyncOptionsType;\n\tsetCount: (c: number) =\u003e void;\n\tset_Count: (c: number) =\u003e void;\n\tsetOptions: (__persistNSyncOptions: PersistNSyncOptionsType) =\u003e void;\n}\n\nconst defaultOptions = { name: \"example\", include: [/count/], exclude: [/^_/] };\n\nexport const useStoreWithOptions = create\u003cStoreWithOptions\u003e(\n\tpersistNSync(\n\t\tset =\u003e ({\n\t\t\tcount: 0,\n\t\t\t_count: 0 /** skipped as it matches the regexp provided */,\n\t\t\t__persistNSyncOptions: defaultOptions,\n\t\t\tsetCount: count =\u003e set(state =\u003e ({ ...state, count })),\n\t\t\tset_Count: _count =\u003e set(state =\u003e ({ ...state, _count })),\n\t\t\tsetOptions: __persistNSyncOptions =\u003e set(state =\u003e ({ ...state, __persistNSyncOptions })),\n\t\t}),\n\t\tdefaultOptions,\n\t),\n);\n```\n\n### Clear Storage\n\nStarting 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.   \n\n```ts\nimport { clearStorage } from \"persist-and-sync\";\n\n...\n\tclearStorage(\"my-store\", \"cookies\");\n...\n```\n\n## Legacy / Deprecated\n\n#### Ignore/filter out fields based on regExp\n\nIn 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 -\u003e undefined) in the options object.\n\n```ts\n// to ignore fields containing a slug\npersistNSync(\n    set =\u003e ({\n      count: 0,\n      slugSomeState: 1,\n      slugSomeState2: 1,\n      set: n =\u003e set({ count: n }),\n    }),\n    { name: \"my-channel\", regExpToIgnore: /slug/ },\n    // or regExpToIgnore: new RegExp('slug')\n    // Use full power of regExp by adding `i` and `g` flags\n  ),\n```\n\nFor more details about regExp check out - [JS RegExp](https://www.w3schools.com/jsref/jsref_obj_regexp.asp)\n\n### Exact match\n\nFor exactly matching a parameter/field use `/^your-field-name$/`. `^` forces match from the first character and similarly, `$` forces match until the last character.\n\n### Ignore multiple fields with exact match\n\nuse `regExpToIgnore: /^(field1|field2|field3)$/`\n\n### 🤩 Don't forget to star [this repo](https://github.com/mayank1513/persist-and-sync)!\n\nWant a hands-on course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://mayank-chaudhari.vercel.app/courses/react-and-next-js-with-typescript) and [The Game of Chess with Next.js, React and TypeScrypt](https://www.udemy.com/course/game-of-chess-with-nextjs-react-and-typescrypt/?referralCode=851A28F10B254A8523FE)\n\n## License\n\nLicensed as MIT open source.\n\n\u003chr /\u003e\n\n\u003cp align=\"center\" style=\"text-align:center\"\u003ewith 💖 by \u003ca href=\"https://mayank-chaudhari.vercel.app\" target=\"_blank\"\u003eMayank Kumar Chaudhari\u003c/a\u003e\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freact18-tools%2Fpersist-and-sync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freact18-tools%2Fpersist-and-sync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freact18-tools%2Fpersist-and-sync/lists"}