Skip to content

Allow configuration for rehydration timeout in useGraphql #37

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

Closed
quantizor opened this issue May 22, 2019 · 9 comments
Closed

Allow configuration for rehydration timeout in useGraphql #37

quantizor opened this issue May 22, 2019 · 9 comments

Comments

@quantizor
Copy link

quantizor commented May 22, 2019

new Date() - firstRenderDate < 500

For server-rendered pages that take a little longer to load, it'd be nice to be able to say for a particular GQL query that for example 5 seconds is acceptable for total load time... or perhaps infinity (takes as long as it takes.)

@jaydenseric
Copy link
Owner

That 500ms limit should work just as effectively regardless of how long the SSR response takes; it only needs to cover how long it takes for the whole app and all nested queries to mount at first render, after all the JS has been downloaded, parsed, and then run on the client.

If the server response takes a long time (say, 6 seconds), the 500ms only kicks in afterwards, when the client app mounts.

If the server response is quick, but the app/route JS bundles take a long time to download and parse (say, 10 seconds), again the 500ms only kicks in after that work has completed.

Are you noticing some issues with post SSR hydration, or were you just pre-empting a possible issue?

@quantizor
Copy link
Author

quantizor commented May 23, 2019

Hmm ok, yeah I'm noticing on one of our heavier pages the 500ms limit is exceeded and it requeries. I guess it just takes a little longer to rehydrate than expected...

@quantizor quantizor changed the title Allow configuration for post-SSR timeout in useGraphql Allow configuration for rehydration timeout in useGraphql May 23, 2019
@jaydenseric
Copy link
Owner

Do you think you will be able to investigate why it takes so long for some parts of your app containing queries to mount, compared to the rest of the app?

There is a tradeoff that if you make the hydration time too long, it might stop legitimate client queries form loading. For example, a user might quickly click on nav link and the new route is supposed to load on mount.

@quantizor
Copy link
Author

I think it's a combo of styled-components rehydration and some third-party JS slowing things down that I don't have a lot of control of... Is it possible to just configure the rehydration timeout per instance of useGraphql?

@mike-marcacci
Copy link

mike-marcacci commented May 23, 2019

@probablyup it sounds like something asynchronous is happening between when react initially mounts with your GraphQLProvider, and when your component with useGraphQL is mounted – perhaps you have a waterfall of requests going on? This is probably not a typical scenario.

That said, I believe you can get the kind of behavior you want by setting loadOnMount to false until you are sure your page is ready.

EDIT: This won't work, as it will trigger a re-render when you switch loadOnMount back to true.

@jaydenseric
Copy link
Owner

jaydenseric commented May 23, 2019

If the first mount takes longer than half a second something funny must be going on that needs to be investigated, and hopefully fixed – renders usually take milliseconds. It seems Styled Components has had performance issues before, so it could be something like that?

The problem with making the timeout longer in the component is that it is not the component itself that takes a long time to mount, but rather it's context amongst ancestors is the problem.

For example, imagine you have a profile component with a query that is used in two pages. In one page, it is deeply nested in strangely slow mounting components. In the other, the mount is speedy and not a problem. Lengthening the hydration timeout within the component to suit the first page would would unsuitably lengthen the timeout for the other page.

Note that if a query that attempts to load on mount within the hydration time that does not have a cache entry yet, it will still fetch data. So I suppose my earlier example of a user quickly navigating to a new route is not a very good one, since that would still fetch as the doesn't doesn't exist in cache yet. Perhaps a better example is a user clicking on a button to refresh a price ticker that already has a cache entry from SSR. Or a button that queries and displays a random quote would show the same quote again and not fetch a fresh quote if clicked quickly within the hydration timeout.

@jaydenseric
Copy link
Owner

jaydenseric commented May 23, 2019

Honestly I'm clutching at straws trying to think of realistic edge-cases where lengthening the 500ms default to something like 1s would cause problems. My last examples are not strictly right either, because if the button just calls load() then it will definitely fetch fresh data even if it's within the hydration time. The hydration time only applies to loading on mount, so an edge case would involve the user attempting to mount something very quickly that already has a cache entry, where you might reasonably expect the data to have changed in such a short period of time.

Would increasing the arbitrary, hardcoded 500ms value to 1000ms solve most of the issues?

@quantizor
Copy link
Author

I’d make it even looser, if you take mobile devices into account. Perhaps 5s, though hopefully no one is building apps that take that long to rehydrate.

@jaydenseric
Copy link
Owner

Adding and documenting an extra option that takes a lot of technical knowledge to understand is not ideal.

I've increased the limit to 1000ms to cover most slower clients.

A little extra loading is no big deal for clients that take a very long time to hydrate; in that situation fixing the root issue, slow hydration and initial render, will also fix the extra loading.

RyanWooldridge added a commit to RyanWooldridge/React-graphql that referenced this issue Aug 21, 2019
denisp22 pushed a commit to denisp22/graphql-react that referenced this issue Jul 27, 2023
DevHawkNov added a commit to DevHawkNov/GraphQL_React that referenced this issue Feb 4, 2025
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

No branches or pull requests

3 participants