Comments (14)
Actually, I think it's better to not rely on isServer
. We really only want to set the initial state if we are in getInitialProps
, and asPath
is only defined when configureStore
is called from getInitialProps
, so this code is better:
export const configureStore = (initialState = {}, { asPath }) => {
const routerMiddleware = createRouterMiddleware()
if (asPath) {
initialState.router = initialRouterState(asPath)
}
const store = createStore(rootReducer, initialState, bindMiddleware([routerMiddleware]))
return store
}
from connected-next-router.
Yes, first example was wrong as it leads to server and client rendering with different redux states, I've deleted it to avoid confusing other people.
I'm going to replace the code in the example, and close this issue.
from connected-next-router.
Ohh, yes, thanks for the heads up!
from connected-next-router.
Alright, I'm able to recreate this by installing next-redux-wrapper: 3.0.0-alpha.0
.
I'm working now on a fix.
FYI: A minimal reproduction is code that I can run on my side to get the same output, e.g. a simple example showing the issue with https://codesandbox.io/
from connected-next-router.
Thank you @danielr18
Sorry for not making a reproduction, I am not very used to it.
from connected-next-router.
It was bit hard to debug, but the issue is that after v2.0.0, they recreate the store in the server and client (see vercel/next.js#4295 (review)) but if you mutate the initialState in the server, which happens when store is recreated but this time outside getInitialProps
, so there's no asPath
, Next.js would serialize the mutated initialState, which differs from what you returned in getInitialProps
, making it a bit confusing.
The flow is something like this:
1 - Server: getInitialProps
=> initialState you expect (with the route you are on)
2 - Server: HOC contructor calls makeStore
again with the initialState, asPath
isn't defined, so router initial state defaults to /
, this mutates initialState from step 1.
3 - Server: Next.js serializes the initialState object.
4 - Client: HOC contructor calls makeStore
again with the initialState (that was mutated in HOC constructor) so it has not the same data that it had when you returned the object from getInitialProps
.
from connected-next-router.
Are you using the initialRouterState
helper?
from connected-next-router.
Yes @danielr18 Here is my store creator function:
export default (initialState = {}, options) => {
if (options.isServer) {
initialState.router = initialRouterState(options.asPath)
}
const store = createStore(combinedReducers, Immutable(initialState), enhancer)
sagaMiddleware.run(rootSaga)
return store
}
from connected-next-router.
Would you mind sharing a minimal reproduction so that I can help further?
It seems to work fine in the example in the repo:
from connected-next-router.
Redux devtools and address bar
Complete redux setup file
import { createRouterMiddleware, initialRouterState, routerReducer } from 'connected-next-router'
import { applyMiddleware, combineReducers, createStore } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly'
import { i18nState } from 'redux-i18n'
import createSagaMiddleware from 'redux-saga'
import Immutable from 'seamless-immutable'
import entities from './entities'
import rootSaga from './sagas'
const reducers = {
i18nState,
router: routerReducer,
}
Object.keys(entities)
.forEach((key) => {
reducers[key] = entities[key].reducer
})
const routerMiddleware = createRouterMiddleware()
const sagaMiddleware = createSagaMiddleware()
const enhancer = composeWithDevTools(applyMiddleware(routerMiddleware, sagaMiddleware))
const combinedReducers = combineReducers(reducers)
export default (initialState = {}, options) => {
if (options.isServer) {
// eslint-disable-next-line
initialState.router = initialRouterState(options.asPath)
}
const store = createStore(combinedReducers, Immutable(initialState), enhancer)
sagaMiddleware.run(rootSaga)
return store
}
export { sagaMiddleware as saga }
from connected-next-router.
I get this when logging as following:
console.log('isServer', options.isServer, ', path:', options.asPath, ', routerState: ', initialState.router)
from connected-next-router.
Here is my temporary workaround
export const makeStore = (initialState = {}, options) => {
if (options.isServer) {
if (options.asPath) {
routerState = initialRouterState(options.asPath)
}
// eslint-disable-next-line
initialState.router = routerState
}
const store = createStore(combinedReducers, Immutable(initialState), enhancer)
sagaMiddleware.run(rootSaga)
return store
}
from connected-next-router.
Your workaround from #9 (comment) made that there was a state flicker during the render but the one from #9 (comment) seems to fix the problem for now
from connected-next-router.
Don't forget to update the Readme too :)
from connected-next-router.
Related Issues (20)
- Support for Next 13/React 18? HOT 5
- ModuleNotFoundError: Module not found: HOT 9
- Doesn't support NextJS 9.0.5 HOT 1
- Example without `next-redux-wrapper` HOT 6
- Requests with query parameters are being redirected to / HOT 4
- Option to change action types names HOT 1
- State updates after the first useEffect(() => {}, []) called. HOT 3
- Feature request: createMatchSelector to replace connected-react-router HOT 8
- my push method from next/router not working properly as expected after wrapping my app with ConnectedRouter HOT 12
- [Question] How to get URL parts in a Reducer? HOT 4
- react-router migration question HOT 2
- search.replace is not a function HOT 3
- Doesn't consider basePath? HOT 3
- Redux-undo?
- Redux Toolkit HOT 3
- Export a NextRouterState type for TypeScript?
- Expose a LocationChangeAction type for TypeScript? HOT 3
- doesn't work with React 18+ HOT 3
- Redux Persist
- peer dependency issue with react-redux@8 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from connected-next-router.