Skip to content
This repository has been archived by the owner on Oct 26, 2018. It is now read-only.

Bug: On server side history change is not propagated to store #284

Closed
svrcekmichal opened this issue Feb 16, 2016 · 6 comments
Closed

Bug: On server side history change is not propagated to store #284

svrcekmichal opened this issue Feb 16, 2016 · 6 comments

Comments

@svrcekmichal
Copy link
Contributor

On server side, accessing any route does not change redux state.

Setup on server side:

const memoryHistory = createMemoryHistory();
const store = createStore(memoryHistory);
const history = syncHistoryWithStore(memoryHistory,store);
const routes = getRoutes(store);

After react-router match store.geState() returned this:

{ routing: 
   { locationBeforeTransitions: 
      { pathname: '/',
        search: '',
        hash: '',
        state: null,
        action: 'POP',
        key: '8j5ti7' } } }

On client side, after syncing history with store, redirect to / is triggered.

Issue can be simulated on this repo svrcekmichal\universal-react
Server side setup is here

@svrcekmichal
Copy link
Contributor Author

syncHistoryWithStore is listening for updates on history.listen. After little test and mounting listen callback I found out that they are not fired in match of react-router when memoryHistory is used

When i have used memoryHistory on client, everything is propagated to the store, so issue probably will be with react-router match not triggering listen

Temporary fix on my side was adding this to react-router match callback

if(renderProps){
  store.dispatch({type:LOCATION_CHANGE,payload:renderProps.location});
}

@gaearon gaearon added the bug label Feb 16, 2016
@timdorr
Copy link
Member

timdorr commented Feb 17, 2016

OK, I'm getting a handle on this finally. I've got a SSR example (that will go up in the next version!) and I'm seeing what both you and @romseguy.

The redirect happens because we're detecting the initial load as a replay and it's attempting to go back to the initial location. That's a bit of red herring, as the actual issue is that they location state generated by match isn't consistent with memory history's default location.

That default location is '/'. So, to fix this, you simply have to set up the history with the current request path. And that's actually really easy:

const memoryHistory = createMemoryHistory(req.path) // Or whatever request path variable koa or others user

That's it! 😄

@timdorr timdorr closed this as completed Feb 17, 2016
@gaearon
Copy link
Member

gaearon commented Feb 17, 2016

Is there any way for us or for match to warn when this happens? Is there any legit scenario when you want them to be out of sync?

@gaearon gaearon removed the bug label Feb 17, 2016
@timdorr
Copy link
Member

timdorr commented Feb 17, 2016

Nope, it's a router/history bug. We shouldn't have a default and should throw when an initial location isn't provided. Working on a fix now.

@taion
Copy link
Member

taion commented Feb 17, 2016

This smells off to me. Why are you syncing history to store when rendering on the server?

@svrcekmichal
Copy link
Contributor Author

@timdorr thanks, now it works as expected

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants