Redux Persist Cookie Storage Adapter
Redux Persist storage adapter for cookies. Works in the browser and in Node.js with cookie-parser output. This makes it suitable for universal / isomorphic applications.
Installation
npm install --save redux-persist-cookie-storage
Usage
Browser
Pure Cookie mode
import { persistStore, persistCombineReducers } from 'redux-persist'
import { CookieStorage } from 'redux-persist-cookie-storage'
import Cookies from 'cookies-js'
const persistConfig = {
key: "root",
storage: new CookieStorage(Cookies)
}
const rootReducer = persistCombineReducers(persistConfig, reducers)
const store = createStore(rootReducer, undefined)
const persistor = persistStore(store, {})
Bootstrap from preloaded state in window object
import { persistStore, persistCombineReducers } from 'redux-persist'
import { CookieStorage } from 'redux-persist-cookie-storage'
import Cookies from 'cookies-js'
const persistConfig = {
key: "root",
storage: new CookieStorage(Cookies),
stateReconciler(inboundState, originalState) {
return originalState
}
}
const rootReducer = persistCombineReducers(persistConfig, reducers)
const store = createStore(rootReducer)
const persistor = persistStore(store, window.PRELOADED_STATE)
Server
import { persistStore, getStoredState } from 'redux-persist'
import { CookieStorage, NodeCookiesWrapper } from 'redux-persist-cookie-storage'
import Cookies from 'cookies'
const app = new Express()
app.use(Cookies.express())
app.use(async (req, res) => {
const cookieJar = new NodeCookiesWrapper(new Cookies(req, res))
const persistConfig = {
key: "root",
storage: new CookieStorage(cookieJar),
stateReconciler(inboundState, originalState) {
return originalState
}
}
let preloadedState
try {
preloadedState = await getStoredState(persistConfig)
}
catch (e) {
preloadedState = {}
}
const rootReducer = persistCombineReducers(persistConfig, reducers)
const store = createStore(rootReducer, preloadedState)
})
import { persistStore, getStoredState } from 'redux-persist'
import { CookieStorage, NodeCookiesWrapper } from 'redux-persist-cookie-storage'
import Cookies from 'cookies'
const configurePersistor = async (store) => {
return new Promise((resolve) => {
const persistor = persistStore(store, {}, () => {
resolve(persistor)
})
})
}
const app = new Express()
app.use(Cookies.express())
app.use(async (req, res) => {
const cookieJar = new NodeCookiesWrapper(new Cookies(req, res))
const persistConfig = {
key: "root",
storage: new CookieStorage(cookieJar),
stateReconciler(inboundState, originalState) {
return originalState
}
}
const rootReducer = persistCombineReducers(persistConfig, reducers)
const store = createStore(rootReducer)
const persistor = await configurePersistor(store)
await persistor.flush()
res.send(200, 'Done!')
})
Options
persistStore(store, { storage: new CookieStorage(Cookies) })
persistStore(store, { storage: new CookieStorage(Cookies, {
expiration: {
'default': 365 * 86400
}
})
})
persistStore(store, { storage: new CookieStorage(Cookies, {
expiration: {
'default': null,
'storeKey': 600
}
})
})
persistStore(store, { storage: new CookieStorage(Cookies, {
setCookieOptions: {
path: '/mypath'
}
})
})
Development
Running tests
npm test