next-persist
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "next-persist", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Bridging the gap between client-side persistence and server-side rendering", | ||
@@ -5,0 +5,0 @@ "main": "src/next-persist.js", |
const nextPersist = {}; | ||
// writes to storage | ||
// writes to local storage | ||
nextPersist.writeStorage = (state) => { | ||
localStorage.setItem('state', JSON.stringify(state)); | ||
if (typeof Window !== 'undefined') { | ||
localStorage.setItem('state', JSON.stringify(state)); | ||
} else { | ||
return state; | ||
} | ||
}; | ||
@@ -10,20 +14,24 @@ | ||
nextPersist.getStorage = (persistConfig, state) => { | ||
const clientState = localStorage.getItem('state'); | ||
if (typeof Window !== 'undefined') { | ||
const clientState = localStorage.getItem('state'); | ||
if (clientState) { | ||
const { allowList } = persistConfig; | ||
const parsedClientState = JSON.parse(clientState); | ||
const newState = allowList.reduce((acc, cur) => { | ||
acc[cur] = parsedClientState[cur]; | ||
return acc; | ||
}, {}); | ||
return { | ||
...state, | ||
...newState, | ||
}; | ||
}; | ||
if (clientState) { | ||
const { allowList } = persistConfig; | ||
const parsedClientState = JSON.parse(clientState); | ||
const newState = allowList.reduce((acc, cur) => { | ||
acc[cur] = parsedClientState[cur]; | ||
return acc; | ||
}, {}); | ||
return { | ||
...state, | ||
...newState, | ||
}; | ||
} | ||
} | ||
return state; | ||
}; | ||
module.exports = nextPersist; |
2939
29