{"id":19522173,"url":"https://github.com/captaincodeman/svelte-web-storage","last_synced_at":"2025-04-26T09:32:05.540Z","repository":{"id":205799898,"uuid":"715259013","full_name":"CaptainCodeman/svelte-web-storage","owner":"CaptainCodeman","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-27T18:59:28.000Z","size":78,"stargazers_count":14,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-04T10:33:54.447Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Svelte","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CaptainCodeman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-11-06T19:30:48.000Z","updated_at":"2025-01-05T11:44:29.000Z","dependencies_parsed_at":"2024-11-11T00:37:33.043Z","dependency_job_id":"51c5352c-b0cc-4f36-95d7-9c435e06737a","html_url":"https://github.com/CaptainCodeman/svelte-web-storage","commit_stats":null,"previous_names":["captaincodeman/svelte-web-storage"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptainCodeman%2Fsvelte-web-storage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptainCodeman%2Fsvelte-web-storage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptainCodeman%2Fsvelte-web-storage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CaptainCodeman%2Fsvelte-web-storage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CaptainCodeman","download_url":"https://codeload.github.com/CaptainCodeman/svelte-web-storage/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250967225,"owners_count":21515563,"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":[],"created_at":"2024-11-11T00:37:30.429Z","updated_at":"2025-04-26T09:32:05.276Z","avatar_url":"https://github.com/CaptainCodeman.png","language":"Svelte","funding_links":[],"categories":[],"sub_categories":[],"readme":"# svelte-web-storage\n\nA [Svelte writable store](https://svelte.dev/docs/svelte-store#writable) that saves values to [Web-Storage ](https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API). Great for persisting settings or preference objects within your Svelte apps. There are lots of packages available that do similar things, see the [comparison](#comparison) for why you might want to use this particular lib.\n\n## Features\n\n- ✅ Tiny size - just 656 bytes minified / 417 bytes minified \u0026 gzipped\n- ✅ Supports `localStorage` for persistence and cross-tab synchronization\n- ✅ Supports `sessionStorage` for independent per-tab values\n- ✅ Store objects, primitive values, and arrays\n- ✅ Customizable serialization (uses JSON by default)\n- ✅ New default properties automatically added to persisted values\n- ✅ Server-side-rendering (SSR) compatible\n\n## Usage\n\n### Installation\n\nInstall using your package manager of choice, e.g.\n\n    pnpm i -D svelte-web-storage\n\n### LocalStorage\n\nImport and create a `Writable` store, just as you would with the default Svelte `writable` but passing in a key name of storage before the default value(s).\n\n```ts\nimport { web_storage } from 'svelte-web-storage'\n\nexport const settings = web_storage('settings, {\n  page_size: 24,\n  currency: 'USD',\n  language: 'en-US',\n})\n```\n\nYour settings can be accessed throughout your app and will be persisted to localStorage and changes to settings will be synchronized across browser tabs.\n\n### SessionStorage\n\nTo use `sessionStorage` which isn't persisted or synchronized across tabs, use a 3rd options parameter to set `persist` to `false`:\n\n```ts\nimport { web_storage } from 'svelte-web-storage'\n\nexport const settings = web_storage('settings, {\n  page_size: 24,\n  currency: 'USD',\n  language: 'en-US',\n}, { persist: false }) // \u003c== disables persistence\n```\n\n### Custom Serialization\n\nPersisted data is stored using `JSON.parse` and `JSON.stringify` but this can be overridden by passing in a `serializer` as part of the 3rd options parameter. This might be because you have some legacy format that you want to use:\n\n```ts\nimport { web_storage } from 'svelte-web-storage'\n\nexport const settings = web_storage('settings, {\n  page_size: 24,\n  currency: 'USD',\n  language: 'en-US',\n}, {\n  serializer: {\n    parse(text: string) {\n      const parts = text.split(':');\n      return {\n        page_size: parseInt(parts[0]),\n        currency: parts[1],\n        language: parts[2]\n      };\n    },\n    stringify(value) {\n      return `${value.page_size}:${value.currency}:${value.language}`;\n    }\n  }\n})\n```\n\n### Upgrading Objects\n\nIf you add new properties to your settings object, the new default values of those properties will be automatically added to any persisted values. Adding a `theme` property to the previous example would set the store value to `system`, but leave any existing customized properties unchanged. No need to manually handle properties missing from the persisted state, or your settings having possibly undefined values.\n\n```ts\nimport { web_storage } from 'svelte-web-storage'\n\nexport const settings = web_storage('settings, {\n  page_size: 24,\n  currency: 'USD',\n  language: 'en-US',\n  theme: 'system',\n})\n```\n\n## Comparison (work in progress)\n\nThere are lots of packages that do similar things to this lib, so why might you want to use this one? I've tried to put together a comparison of all the ones I could find - I'm not claiming it's an exhaustive or a 100% accurate list, so let me know if there is something I've missed or you think is incorrect.\n\nThe criteria for comparing includes:\n\n- **Byte size** of package\n  - what impact will using the lib have on your application bundle size (using https://bundlephobia.com/)\n- **Correctness**\n  - stores should correctly handle unsubscribing to prevent bugs and memory leaks\n- **Upgradeable**\n  - does it handle adding new properties without overwriting the defaults?\n- **Server-Side-Rendering** (SSR) compatibility\n  - older libs that pre-date SvelteKit often lack support for SSR\n- **Svelte Compatible**\n  - Can it be used outside of SvelteKit (does it use any $app dependencies)\n- **Web-Storage**\n  - are both localStorage _and_ sessionStorage types of web-storage supported?\n- **Synchronization of browser tabs**\n  - are changes in one tab reflected in another when using localStorage?\n- **objects, primitive values, and Arrays**\n  - can it be used with primitive values, objects, and arrays\n- **TypeScript** Support\n  - are typings provided\n- **Custom Serialization**\n  - JSON is a sensible default but can it be overridden?\n- **Handsomeness** of the author\n  - that's a joke, to see if anyone read this far ...\n\n| Name                                | Version | Minified | GZipped | Correct | Upgrade | SSR | SK Deps | Session | Sync | Values | TS  | Serialize |\n| ----------------------------------- | ------- | -------: | ------: | :-----: | :-----: | :-: | :-----: | :-----: | :--: | :----: | :-: | :-------: |\n| svelte-web-storage                  | 0.0.2   |     656B |    417B |   ✅    |   ✅    | ✅  |   ✅    |   ✅    |  ✅  |   ✅   | ✅  |    ✅     |\n| svelte-persisted-store              | 0.7.0   |   1.24kB |    650B |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| svelte-persistent-store             | 0.1.6   |    1.7kB |    837B |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| svelte-backed-store                 | 1.1.1   |    3.5kB |  1.25kB |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| svelte-persistent-writable          | 1.1.6   |    1.4kB |    631B |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| svelte-localstorage-writable        | 0.1.3   |     960B |    519B |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| svelte-syncable                     | 1.0.4   |          |         |   ❌    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| svelte-storable                     | 1.0.4   |      1kB |    509B |   ✅    |   ❓    | ❌  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| svelte-cached-store                 |         |          |         |   ❌    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| @macfja/svelte-persistent-store     | 2.4.1   |   19.9kB |   7.3kB |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| @macfja/browser-storage-store       | 1.0.0   |    4.2kB |   1.9kB |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| @n0n3br/svelte-persist-store        | 1.0.2   |    8.4kB |   2.9kB |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| @babichjacob/svelte-localstorage    |         |          |         |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| @typhonjs-svelte/simple-web-storage |         |          |         |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| @thegrommet/svelte-syncable         | 2.0.0   |     846B |    447B |   ❌    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n| @furudean/svelte-persistent-store   | 0.8.0   |     881B |    494B |   ✅    |   ❓    | ❓  |   ❓    |   ❓    |  ❓  |   ❓   | ❓  |    ❓     |\n\n### Methodology:\n\n- Check source-code for subscription handling pluse dependencies on SvelteKit.\n- Tested each library by importing, creating a test page, and seeing how it handled both SSR, objects vs values, adding properties, etc...\n- Used [Bundlephobia](https://bundlephobia.com/) to check the size of packages, falling back to published NPM distributed size.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaptaincodeman%2Fsvelte-web-storage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcaptaincodeman%2Fsvelte-web-storage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcaptaincodeman%2Fsvelte-web-storage/lists"}