{"id":22621662,"url":"https://github.com/larscom/ngrx-signals-storage","last_synced_at":"2025-09-09T11:10:42.036Z","repository":{"id":213956929,"uuid":"735027600","full_name":"larscom/ngrx-signals-storage","owner":"larscom","description":"Save signal state (@ngrx/signals) to localStorage/sessionStorage and restore the state on page load with a single line of code (with SSR support)","archived":false,"fork":false,"pushed_at":"2025-09-03T20:45:53.000Z","size":1806,"stargazers_count":10,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-03T22:23:35.249Z","etag":null,"topics":["angular","localstorage","ngrx","reactive","redux","sessionstorage","signal-store","signals","signalstore","state","storage","store"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@larscom/ngrx-signals-storage","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/larscom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-12-23T12:05:31.000Z","updated_at":"2025-09-03T20:45:57.000Z","dependencies_parsed_at":"2024-11-22T00:00:08.563Z","dependency_job_id":"4100ff27-03f5-4b2b-978d-71af58095b06","html_url":"https://github.com/larscom/ngrx-signals-storage","commit_stats":{"total_commits":64,"total_committers":3,"mean_commits":"21.333333333333332","dds":0.484375,"last_synced_commit":"e5646193919bd603b38f6bd04732700e9b7dacb6"},"previous_names":["larscom/ngrx-signals-storage"],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/larscom/ngrx-signals-storage","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larscom%2Fngrx-signals-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larscom%2Fngrx-signals-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larscom%2Fngrx-signals-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larscom%2Fngrx-signals-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/larscom","download_url":"https://codeload.github.com/larscom/ngrx-signals-storage/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/larscom%2Fngrx-signals-storage/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274288707,"owners_count":25257769,"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","status":"online","status_checked_at":"2025-09-09T02:00:10.223Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["angular","localstorage","ngrx","reactive","redux","sessionstorage","signal-store","signals","signalstore","state","storage","store"],"created_at":"2024-12-08T23:11:15.678Z","updated_at":"2025-09-09T11:10:42.028Z","avatar_url":"https://github.com/larscom.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @larscom/ngrx-signals-storage\n\n[![npm-version](https://img.shields.io/npm/v/@larscom/ngrx-signals-storage.svg?label=npm)](https://www.npmjs.com/package/@larscom/ngrx-signals-storage)\n![npm](https://img.shields.io/npm/dw/@larscom/ngrx-signals-storage)\n[![license](https://img.shields.io/npm/l/@larscom/ngrx-signals-storage.svg)](https://github.com/larscom/ngrx-signals-storage/blob/main/LICENSE)\n\n\u003e Save signal state (@ngrx/signals) to localStorage/sessionStorage and restore the state on page load with a single line of code (with SSR support).\n\n## Installation\n\n```bash\nnpm install @larscom/ngrx-signals-storage\n```\n\n## Dependencies\n\n`@larscom/ngrx-signals-storage` depends on [@ngrx/signals](https://ngrx.io/guide/signals/install) and [Angular](https://github.com/angular/angular)\n\n## Usage\n\nImport the `withStorage` function and place it after the `withState` function. Optional configuration can be passed as 3th argument.\n\n```ts\nimport { withStorage } from '@larscom/ngrx-signals-storage'\nimport { withState, signalStore } from '@ngrx/signals'\n\nexport const CounterStore = signalStore(\n  withState({\n    count: 0\n  }),\n  // state will be saved to sessionStorage under the key: 'myKey'\n  withStorage('myKey', () =\u003e sessionStorage)\n)\n```\n\n## Configuration\n\n```ts\nexport interface Config\u003cT\u003e {\n  /**\n   * These keys will not get saved to storage\n   */\n  excludeKeys: Array\u003ckeyof T\u003e\n\n  /**\n   * Serializer for the state, by default it uses `JSON.stringify()`\n   * @param state the last state known before it gets saved to storage\n   */\n  serialize: (state: T) =\u003e string\n\n  /**\n   * Deserializer for the state, by default it uses `JSON.parse()`\n   * @param state the last state known from the storage location\n   */\n  deserialize: (state: string) =\u003e T\n\n  /**\n   * Save to storage will only occur when this function returns true\n   * @param state the last state known before it gets saved to storage\n   */\n  saveIf: (state: T) =\u003e boolean\n\n  /**\n   * Function that gets executed on a storage error (get/set)\n   * @param error the error that occurred\n   */\n  error: (error: any) =\u003e void\n}\n```\n\n## Save conditionally\n\nSometimes you only want to save to storage on a specific condition.\n\n```ts\nimport { withStorage } from '@larscom/ngrx-signals-storage'\nimport { withState, signalStore } from '@ngrx/signals'\n\nexport const CounterStore = signalStore(\n  withState({\n    count: 0\n  }),\n  // save only occurs when count is higher than 0\n  withStorage('myKey', () =\u003e sessionStorage, { saveIf: ({ count }) =\u003e count \u003e 0 })\n)\n```\n\n## Skip properties\n\nSometimes you want to ignore / exclude properties so they do not get saved into storage. On page reload, the initial value will be loaded instead.\n\n```ts\nimport { withStorage } from '@larscom/ngrx-signals-storage'\nimport { withState, signalStore } from '@ngrx/signals'\n\nexport const CounterStore = signalStore(\n  withState({\n    count: 0,\n    sum: 0\n  }),\n  // sum does not get saved into sessionStorage.\n  withStorage('myKey', () =\u003e sessionStorage, { excludeKeys: ['sum'] })\n)\n```\n\n## Common Errors\n\nWhenever you get errors this is most likely due to serialization / deserialization of the state.\n\nObjects like `Map` and `Set` are not serializable so you might need to implement your own serialize / deserialize function.\n\n### Serialize / Deserialize\n\nLets say you have a `Set` in your store, then you need a custom serialize / deserialize function to convert from `Set` to `Array` (serialize) and from `Array` to `Set` (deserialize)\n\n```ts\nexport const MyStore = signalStore(\n  withState({\n    mySet: new Set([1, 1, 3, 3])\n  }),\n  withStorage('myKey', () =\u003e sessionStorage, {\n    serialize: (state) =\u003e JSON.stringify({ ...state, mySet: Array.from(state.mySet) }),\n    deserialize: (stateString) =\u003e {\n      const state = JSON.parse(stateString)\n      return {\n        ...state,\n        mySet: new Set(state.mySet)\n      }\n    }\n  })\n)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flarscom%2Fngrx-signals-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flarscom%2Fngrx-signals-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flarscom%2Fngrx-signals-storage/lists"}