The complex world where suspended and not suspended queries coexist #1147
Replies: 1 comment 5 replies
-
Yeah this is interesting. The way it works today is all based on promise sharing between queries with identical keys. So if you have a suspense boundary that depends on a promise being resolved in react query, only to display another instance of the same query inside of it, then you are correct that this will trigger another fetch, but only if the data is stale. In that example, it technically would be no different if you weren’t using suspense. We’ve been well aware of this from the beginning and have prescribed staleTime adjustments to your query instance configs. Theoretically, if your data is changing very quickly, the second fetch would reveal new data, and likewise, if you know that’s highly unlikely, then you can and should increase the stale time regardless to match your expectation or requirements of data freshness. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In a project I'm working on we have some queries that use
suspense: true
and some that usesuspense: false
.We discovered that in such a setup request de-duplication behaves differently than what we expected. Some queries are fetched multiple times even though the suspended component renders just once.
This codesandbox illustrates the behavior I'm talking about - https://codesandbox.io/s/condescending-mccarthy-xpu9x?file=/src/App.js The whole
App
is suspended, and parameters don't change, so we'd expect a result like:Instead, we get:
What means that the following queries are run twice:
I'm not familiar with the current codebase, but I've studied the v1 version. The observed behavior is identical, so I'll assume it conceptually stayed the same.
What we observe here is that the
"second"
query starts before"first"
"gets suspended" and then it goes stale. Because it's stale, when the components finally render without getting suspended, "second" is refetched.It's the correct behavior if we agree that queries are the things that get suspended. However, there's another way to think about it - where components, and not individual hooks, get suspended.
If we agreed that components get suspended, and in the linked codesandbox all components appear on the screen at once, then both queries should be fetched only once.
I'd love to know your opinion about it. What is the expected behavior? Which one adheres to the Principle Of Least Astonishment?
Beta Was this translation helpful? Give feedback.
All reactions