Skip to content

Commit

Permalink
(fix): do not error if 'window' is undefined
Browse files Browse the repository at this point in the history
- in certain environments like Node, 'window' will not be defined and
  so will cause mst-persist to error out on its check for localStorge,
  even if a different storage engine were configured

- this fix enables usage in Node environments, supporting usage like
  hydrating server-side
  • Loading branch information
barbalex authored and agilgur5 committed Jul 27, 2019
1 parent bd37065 commit 63053b9
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ 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 (!jsonify) { jsonify = true } // default to true like mobx-persist
Expand Down

0 comments on commit 63053b9

Please sign in to comment.