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

Get rid of ssrForceFetchDelay #4814

Closed
darkowic opened this issue May 14, 2019 · 3 comments
Closed

Get rid of ssrForceFetchDelay #4814

darkowic opened this issue May 14, 2019 · 3 comments

Comments

@darkowic
Copy link

ssrForceFetchDelay is a huge workaround for SSR. It is not working if you set too small value especially if you have any asynchronous initialization e.g. react-loadable. I've just spent some time fixing this problem in my project and realized that there is an obvious solution on how to tell apollo-client to not fetch data from API on the first load. The code will explain everything:

client.disableNetworkFetches = true;
ReactDOM.hydrate(
  <ApolloProvider client={client}>
    <MyApp />
  </ApolloProvider>,
  MOUNT_NODE,
  () => {
    // app is hydrated!
    client.disableNetworkFetches = false;
  }
)

React DOM hydrate and render receives as a 3rd argument a callback which works the same as componentDidMount. When the callback is called all components are already rendered.

We can go further and do this in ApolloPrivider

class ApolloProvider extends React.Component {
  constructor(props) {
    super(props);
    props.client.disableNetworkFetches = true;
  }

  componentDidMount() {
    this.props.client.disableNetworkFetches = false;
  }
  ...
}

What do you think?
❤️ this project!

@mikebm
Copy link

mikebm commented Aug 1, 2019

This is way better than the setTimeout that ssrForceFetchDelay does. I've had to set our apps delay time to 1000. This solution has solved our constant delay tweaking. Thanks for posting it.

@pawsong
Copy link

pawsong commented Mar 27, 2020

Made a hooks version of this approach:

https://github.com/specup/next-with-apollo-hoc/blob/20cec7e20bfe919c6ccc56cb2b47d479a7c678c6/pages/_app.tsx#L27

jimrandomh added a commit to ForumMagnum/ForumMagnum that referenced this issue Jul 14, 2020
As suggested in apollographql/apollo-client#4814 ,
ssrForceFetchDelay is bad because it can lead to cascading refetches on
load, if something being unexpectedly slow pushes the timeline past the
delay duration. Instead, explicitly disable and reenable network fetches
before and after rehydration.
@willwill96
Copy link

This is far better than the ssrForceFetchDelay option. Thank you @pawsong for hooks approach

@apollographql apollographql locked and limited conversation to collaborators Apr 27, 2021
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

5 participants