@macfja/svelte-persistent-store

A Svelte store that keep its value through pages and reloads

Usage no npm install needed!

<script type="module">
  import macfjaSveltePersistentStore from 'https://cdn.skypack.dev/@macfja/svelte-persistent-store';
</script>

README

Svelte Persistent store

A Svelte store that keep its value through pages and reloads

Installation

npm install @macfja/svelte-persistent-store

Usage

import { persist, localStorage } from "@macfja/svelte-persistent-store"
import { writable } from "svelte/store"

let name = persist(writable('John'), localStorage(), 'name')

$name = 'Jeanne Doe'

// if you reload the page the value of $name is 'Jeanne Doe'

Storages

There are 4 storages built-in:

  • localStorage(), that use window.localStorage to save values
  • sessionStorage(), that use window.sessionStorage to save values
  • cookieStorage(), that use document.cookie to save values
  • indexedDBStorage(), that use window.indexedDB to save values

You can add more storages, you just need to implement the interface StorageInterface

Documentation

Documentation and examples car be generated with npm run doc, next open docs/index.html with your favorite web browser.

(Hint: If you don't want to generate the docs, a part of the example and documentation are available here)

Contributing

Contributions are welcome. Please open up an issue or create PR if you would like to help out.

Read more in the Contributing file

License

The MIT License (MIT). Please see License File for more information.