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

Duplicate useQuery()s in the same component causes "Invariant Violation: Too many re-renders" during SSR #457

Closed
ThomasJClark opened this issue Oct 24, 2019 · 7 comments · Fixed by #459
Labels
bug 🐛 Oh no! A bug or unintented behaviour.

Comments

@ThomasJClark
Copy link

Describe the bug
After upgrading to urql 1.6.1, a component with two useQuery calls that previously worked is now causing React to throw invariant violation errors during SSR.

Steps to reproduce

In a functional component, call useQuery with the same query and variables more than once.

const MyComponent = () => {
  const [res] = useQuery({ query: DemoQuery, variables: { id: 569188001 } });
  const [res2] = useQuery({ query: DemoQuery, variables: { id: 569188001 } });
  return null;
}

Expected behavior
The component renders with res.data and res2.data containing the same data

Actual behavior
React throws an error Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop.

@ThomasJClark ThomasJClark added the bug 🐛 Oh no! A bug or unintented behaviour. label Oct 24, 2019
@samkline
Copy link

samkline commented Oct 24, 2019

Just to speculate a bit, we may not be able to reproduce this on the client because we use the dedupe exchange there. During server-side rendering, we do not use the dedupe exchange, and instead have a custom exchange to handle de-duping, batching, and execution.

This didn't cause a render loop in Urql 1.5.0, so I suspect the recent changes away from useImmediateState/useImmediateEffect caused it.

Edit: I added the dedupe exchange to SSR and it resolved one of the two instances of this problem. The two instances are on distinct pages and in distinct components so there are likely to be other differences between them.

@kitten
Copy link
Member

kitten commented Oct 25, 2019

That’s very interesting. I suppose it could be caused by updates that are being accidentally triggered on both hooks that keep triggering more mutual updates between them.

I’ll look into this and will see whether there’s an easy solution for this problem!

@kitten kitten added the needs more info ✋ A question or report that needs more info to be addressable label Oct 25, 2019
@kitten
Copy link
Member

kitten commented Oct 26, 2019

Hiya, on second thought, we may need a reproduction here. Your custom exchange sounds rather interesting and I’m wondering whether that’s what’s causing a weird interaction with the changes we’ve made and the ssrExchange.

@marcusdarmstrong
Copy link

Well, as it happens we've never gotten around to masking our jsx source from the public internet, so: https://www.tripadvisor.com/components/packages/platform/urql/ssrFetchExchange.jsx

That's the only exchange we've configured for our SSR pass.

@kitten kitten removed the needs more info ✋ A question or report that needs more info to be addressable label Oct 29, 2019
@kitten
Copy link
Member

kitten commented Oct 29, 2019

Alright, I think I've finally narrowed down the problem and have a PR with a fix open, so this will be fixed in the next patch release

@samkline
Copy link

Thanks for the quick fix, @kitten !

@kitten
Copy link
Member

kitten commented Nov 5, 2019

This has now been published as part of https://github.com/FormidableLabs/urql/releases/tag/v1.6.2
Sorry for the delay! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Oh no! A bug or unintented behaviour.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants