-
Notifications
You must be signed in to change notification settings - Fork 140
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
Usage with redux-persist #68
Comments
I don't currently know, but I need to find out. Can you try applying it directly to the demo in isolation and see if there are any problems: https://github.com/faceyspacey/redux-first-router-demo Probably the best way to figure out how redux-persist plays with SSR + rehydration. |
I implemented it along with RFR and everything works as expected. There will be actions being fired before the re-hydration happens so you will need to rather use this or this. @faceyspacey it would be nice to have it on the demo! |
Here's how I solved it. Maybe it's helpful to somebody. Had to use the cookies adapter for redux-persist so I had the state I'm interested in on the server already. Not sure if it's really the best solution but it works for me :) // src/routesMap.js
// …
thunk: (dispatch, getState) => {
const { steps } = getState() // up to you to handle via standard redux techniques
canContinueIf(steps.someStepCompleted, dispatch)
}
// …
const canContinueIf = (stepCompleted, dispatch) => {
if (!stepCompleted) {
const action = redirect({ type: 'SOME_DEFAULT_ROUTE' })
dispatch(action)
}
} // server/configureStore.js
export default async (req, res) => {
// …
const cookies = req.cookies
const parsedCookies = {}
Object.entries(cookies).forEach(([key, value]) => {
const decodedKey = decodeURIComponent(key)
const keyWithoutReduxPersistPrefix = decodedKey.replace(/reduxPersist:/, '')
if (key !== 'reduxPersistIndex') {
parsedCookies[keyWithoutReduxPersistPrefix] = JSON.parse(value)
}
})
const preLoadedState = { ...parsedCookies } // onBeforeChange will authenticate using this
// …
} // src/configureStore.js
export default (history, preLoadedState) => {
// …
const store = createStore(rootReducer, preLoadedState, enhancers)
if (!isServer) {
persistStore(store, {
blacklist: ['location', 'page'],
storage: new CookieStorage()
})
}
// …
} // server/index.js
import cookieParser from 'cookie-parser'
// …
app.use(cookieParser())
// … |
excellent! ...i think it's likely the only way--how else are we going to guarantee the client state becomes redux state on the server? Headers doesn't work, since direct visits can't contain headers like ajax requests in SPAs. Cookies is the only option. Very well done. I'll probably even document this approach at some point. If you're up to, feel free to make a pr with a new |
...perhaps a more formal solution/package would let you blacklist keys so huge amounts of domain state isn't stored in cookies (which would slow down requests). We only want UI state after all. Or perhaps there is a reason to have domain state--black/whitelist options would let the user determine what should be stored in cookies. |
@ganlub so the idea is that to buffer and delay actions that enhancers like the one from RFR might trigger, so that when route |
@faceyspacey created a little doc locally. tried creating a feature branch, commiting with |
figured it out, here goes: #78 |
excellent. was just writin. |
im just gonna close this, since we have the PR (doing a little cleanup) |
I know you closed this, @faceyspacey, but I wanted to add a few more details that I've been teasing out over the past hour. First, I liked the idea of using I've created a tentative pull request in Using this approach, if we set up our const middlewares = applyMiddleware(
createActionBuffer(REHYDRATE, null, [PERSIST]),
middleware,
thunk,
logger
) I get the desired be behavior and my action sequence looks like: Does that make sense and do you see any issues with this? I'm really only messing around with it in the browser (not native or server at this point). It's possible I've missed a bigger problem here but this seems to solve all of my issues with not having access to the persisted store in It should probably be stated clearly that I haven't yet had a chance to see how this should work with the universal suite you've been working on. My very limited use case is that I am making an ajax request to a lambda function which returns a jwt that I'd like to store in Also, thanks for the great work on this router! |
Thanks for taking the time to do this analysis! ...so i havent had time to focus on redux-persist personally unfortunately. So that means I'm lacking in answers. But I can say this: i dont really like the fact that now u gotta add So to me that's usually a signal to dig my heals in at some point and take a holistic approach and see if we can do "better." I still can barely wrap my head around what the buffer thing is doing since i havent tried it--I get it, it's delaying actions. But maybe there's a better way. Why can't we just get a damn callback or Feel free to let me know if I'm far off in my limited birds eye perspective. |
No problem. Just thought it might be helpful to someone else, but I agree it feels like a hack and should be viewed as a stop-gap solution. And I'd love to have all of this become effortless, as do you! I don't think you're far off in seeing that this approach isn't ideal, but I'm not really sure how an await/hook into redux-persist would look. Maybe the Then we could hook into that inside My PR should be merged shortly though over on rt2zz/redux-action-buffer#15 so that solution should work for now for my and maybe other use cases. |
@faceyspacey I ended up going with the In case it helps somebody, export default (state = LOADING, action = {}) => {
return components[action.type] || state
}
const components = {
ROUTE_HOME,
ROUTE_LOGIN,
NOT_FOUND
} |
@ganlub Are you using the v5 branch of redux-persist? If so, I couldn't find the callback as I believe it's being removed. A few people have suggested adding it back, which I would be in favor of. |
@itslittlejohn Oh I see, no I'm using v4.8.3. |
Not to worry though. Seems like the callback behavior is coming back: rt2zz/redux-persist#408 |
I tried out the callback that you pass to I need to do something every time EDIT: The action buffer actually only fires the callback once as well, when it releases the buffer. So, I ended up just handling |
How would I use this with
redux-persist
?The text was updated successfully, but these errors were encountered: