Skip to content
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

fix(using-redux): create a fresh store for each SSR page #11134

Merged
merged 3 commits into from
Feb 5, 2019
Merged

fix(using-redux): create a fresh store for each SSR page #11134

merged 3 commits into from
Feb 5, 2019

Conversation

cpboyd
Copy link
Contributor

@cpboyd cpboyd commented Jan 17, 2019

Description

As I'd mentioned in #10912, the current using-redux example creates a static store that persists across multiple SSR pages.

It seems that the expected behavior should likely be that each page's initial load is from a fresh store, in order to match the behavior of the browser rendering.

Related Issues

Related to #10912

@wardpeet
Copy link
Contributor

Could you explain:

It seems that the expected behavior should likely be that each page's initial load is from a fresh store, in order to match the behavior of the browser rendering.

Creating a new store for each page is something you don't really want when gatsby re-hydrates your page as the redux store should be shared across pages.

@cpboyd
Copy link
Contributor Author

cpboyd commented Jan 18, 2019

@wardpeet Yes, the redux store should be shared across pages once the page has been loaded, but not for every initial rendering of every single page during gatsby build.

Currently, as mentioned in #10912, each page gets rendered retaining the prior state since the store is created static and thus retained between each initial rendering in the gatsby build process.

@cpboyd
Copy link
Contributor Author

cpboyd commented Jan 18, 2019

From #10912, based on the "using-redux" example from this repo:
https://github.com/cpboyd/gatsby-redux-bug

With gatsby develop, all pages initially display a 1 if they're the first page loaded.

However, gatsby build results in the following being rendered (before Javascript browser rendering takes over):

  1. A
  2. B
  3. C
  4. Home

Note: I'd initially tried using componentWillMount(), which apparently doesn't get called during SSR in React.

@cpboyd
Copy link
Contributor Author

cpboyd commented Jan 22, 2019

A few new remarks:
Redux's help indicates SSR should create a new store for each page, but the client is static:
https://redux.js.org/recipes/server-rendering#serverjs-1

As a result, maybe we should just go back to having separate functions for gatsby-browser.js (which would match the prior wrapWithProvider and gatsby-ssr.js which would create a new store for each rendered page.

Further, even placing redux state changes in the constructor() doesn't necessarily result in the state change being dispatched BEFORE rendering occurs.

Thus, it might be worth updating the example to show pages with differing initial states.

e.g., I'm currently using:

const getStateFor = (pathname)  => {
  if (!pathname) {
    return {};
  }
...<computation here>
  return {
    ...<computed values>
  }
}

const createStore = (pathname) => {
  const pathState = getStateFor(pathname);
  return reduxCreateStore(reducer,
    {
      ...initialState,
      ...pathState
    }, composeWithDevTools(applyMiddleware()));
}

@pieh
Copy link
Contributor

pieh commented Jan 25, 2019

the wrapRootElement is called just once in gatsby-browser (just before initial render), so this change is safe to do, but we could adjust comment there a bit, to make it clear that it will cause fresh store instance for each SSR page, but it will only be called once in browser

From pieh's suggestion

Co-Authored-By: cpboyd <xpboyd@gmail.com>
@cpboyd
Copy link
Contributor Author

cpboyd commented Feb 5, 2019

@pieh updated with your suggestion

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cpboyd!

@pieh pieh merged commit ec8deb2 into gatsbyjs:master Feb 5, 2019
@gatsbot
Copy link

gatsbot bot commented Feb 5, 2019

Holy buckets, @cpboyd — we just merged your PR to Gatsby! 💪💜

Gatsby is built by awesome people like you. Let us say “thanks” in two ways:

  1. We’d like to send you some Gatsby swag. As a token of our appreciation, you can go to the Gatsby Swag Store and log in with your GitHub account to get a coupon code good for one free piece of swag. (Currently we’ve got a couple t-shirts available, plus some socks that are really razzing our berries right now.)
  2. We just invited you to join the Gatsby organization on GitHub. This will add you to our team of maintainers. Accept the invite by visiting https://github.com/orgs/gatsbyjs/invitation. By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests.

If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’.

Thanks again!

pragmaticpat pushed a commit to pragmaticpat/gatsby that referenced this pull request Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants