Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Support for Node and Add Docs for Node and SSR #15

Merged
merged 5 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,33 @@ persist('some', someStore, {
- **key** *string* The key of your storage engine that you want to persist to.
- **store** *MST store* The store to be persisted.
- **options** *object* Additional configuration options.
- **storage** *[localForage](https://github.com/localForage/localForage) / AsyncStorage / localStorage* [localForage](https://github.com/localForage/localForage)-style storage API. localStorage for Web (default), AsyncStorage for React Native.
- **storage** *[localForage](https://github.com/localForage/localForage) / AsyncStorage / localStorage*
Any Storage Engine that has a Promise-style API similar to [`localForage`](https://github.com/localForage/localForage).
The default is `localStorage`, which has a built-in adaptor to make it support Promises.
For React Native, one may configure `AsyncStorage` instead.
<br>
Any of [`redux-persist`'s Storage Engines](https://github.com/rt2zz/redux-persist#storage-engines) should also be compatible with `mst-persist`.
- **jsonify** *bool* Enables serialization as JSON (default: `true`).
- **whitelist** *Array\<string\>* Only these keys will be persisted (defaults to all keys).
- **blacklist** *Array\<string\>* These keys will not be persisted (defaults to all keys).

- returns a void Promise

### Node and Server-Side Rendering (SSR) Usage

Node environments are supported so long as you configure a Storage Engine that supports Node, such as [`redux-persist-node-storage`](https://github.com/pellejacobs/redux-persist-node-storage), [`redux-persist-cookie-storage`](https://github.com/abersager/redux-persist-cookie-storage), etc.
This allows you to hydrate your store server-side.

For SSR though, you may not want to hydrate your store server-side, so in that case you can call `persist` conditionally:

```javascript
if (typeof window !== 'undefined') { // window is undefined in Node
persist(...)
}
```

With this conditional check, your store will only be hydrated client-side.

## Examples

None yet, but can take a look at [agilgur5/react-native-manga-reader-app](https://github.com/agilgur5/react-native-manga-reader-app) which uses it in production.
Expand Down
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ type StrToAnyMap = {[key: string]: any}
export const persist: IArgs = (name, store, options = {}) => {
let {storage, jsonify, whitelist, blacklist} = options

if (typeof window.localStorage !== 'undefined' && (!storage || storage === window.localStorage)) {
// use AsyncLocalStorage by default (or if localStorage was passed in)
if (
typeof window !== 'undefined' &&
typeof window.localStorage !== 'undefined' &&
(!storage || storage === window.localStorage)
) {
storage = AsyncLocalStorage
}
if (!storage) {
return Promise.reject('localStorage (the default storage engine) is not ' +
'supported in this environment. Please configure a different storage ' +
'engine via the `storage:` option.')
}

if (!jsonify) { jsonify = true } // default to true like mobx-persist
const whitelistDict = arrToDict(whitelist)
const blacklistDict = arrToDict(blacklist)
Expand Down