https://github.com/jamen/hyperapp-persist
Persist an app's state to the next session.
https://github.com/jamen/hyperapp-persist
Last synced: 11 months ago
JSON representation
Persist an app's state to the next session.
- Host: GitHub
- URL: https://github.com/jamen/hyperapp-persist
- Owner: jamen
- Created: 2017-06-12T21:39:21.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-10T14:01:12.000Z (over 8 years ago)
- Last Synced: 2025-01-12T11:43:42.758Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 39.1 KB
- Stars: 18
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# hyperapp-persist
> Persist an app's state between sessions
Use this [Hyperapp](https://github.com/hyperapp/hyperapp) HOA (Higher-Order App) to persist the state between sessions. As the page unloads the state is saved in `localStorage`, and then the it is restored on the next load.
## Install
```sh
npm i hyperapp-persist
```
## Usage
### `persist(app, options)`
Persist is a Higher-Order App that bootstraps the `app()`.
The options required are:
- `storage` where the state is saved on `localStorage`
- `include` to specify what state gets saved
```js
import { app } from 'hyperapp'
import persist from 'hyperapp-persist'
app = persist(app, {
storage: 'my-app/v1',
include: [ 'router', 'player' ]
})
app({
// ...
})
```
Also possible to use environment variables to remove it in production:
```js
if (process.env.DEV) {
app = persist(app, { ... })
}
```
### Versioning the storage
Sometimes when developing an app, you'll change the state in a way incompatible with what all your users have saved. In this case, it is recommended you version the `storage` key. For example:
```js
app = persist(app, {
storage: 'my-app/1',
include: [ 'inputs', 'router', ... ]
})
```
Then if a breaking change is made you increment the number to `my-app/2` and so on.