diff --git a/README.md b/README.md index 5ff2153..ae2308f 100644 --- a/README.md +++ b/README.md @@ -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. +
+ 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\* Only these keys will be persisted (defaults to all keys). - **blacklist** *Array\* 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. diff --git a/src/index.ts b/src/index.ts index c9f4e33..9d0cbff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)