Replies: 3 comments 6 replies
-
Yeah, this is interesting. Shared query context between query instances is something that has been interesting in RQ from the beginning. What I've always strived for is a system where lower-priority requests do not interfere with higher priority requests. For example, if you have a cache-first query that occasionally gets updated because of the same query being used in an instance set up as a network-only query, then I think that is preferred. However, if you have a network-only query that somehow stops requesting because of the same query being configured in another instance to be cache-first, we've got a problem. And if that's what's happening today, then we should work on fixing it. A good example of this "spirit" is the way that refetchIntervals currently work. Multiple instances can have their own interval timings, but the fastest one always takes precedence. We should do the same with query refetching strategies. As far as I know the worst scenario I see in this is where you have a At the end of the day, I think this is okay. We can always improve on making each query instance behave as independently as possible, but at the end of the day, if they cache on the same key, they need to play nice with each other and the user needs to understand the implications of defining contradicting query and data volatility configurations. |
Beta Was this translation helpful? Give feedback.
-
Hey @tannerlinsley 👋
That's why I told partially 🙂. Of course it shouldn't destroy other queries. From other side at first glance the hook below gives exact network-only behavior. hook snippetexport function useQuery(...args) {
const [queryKey, config = {}] = useQueryArgs(args)
const queryInfo = useBaseQuery(queryKey, config)
// Dirty hack
const [noCache, setNoCache] = useState({ noCache: !config.cache && queryInfo.isStale })
if (noCache) {
queryInfo.data = undefined
const overrides = { status: 'loading', ...getStatusBools('loading') }
Object.assign(queryInfo, overrides)
}
const { refetch } = queryInfo
useEffect(() => {
if (noCache) {
refetch().finally(() => {
setNoCache(false)
})
}
}, [noCache, refetch])
handleSuspense(queryInfo)
return queryInfo
}
So IMO with network-only it's a matter of component behavior, not query. The same as for refetch interval. |
Beta Was this translation helpful? Give feedback.
-
Yeah I also think the instance/query behaviour could be a bit confusing to some users. Say for example, we have a user resource on the server:
With each field having different caching characteristics. While not recommended, sometimes this is the API you are given. In a top navigation we then show the username, but because we know this field will never change, we define the query as:
As long as only the top navigation is rendered, we never want to revalidate. Then the user navigates to a page to view his messages. Here we want to show the cached data, but because messages could be updated more frequently, we also want to revalidate and define the query on the messages page as:
As long as the messages page is rendered, we want to revalidate after 10 seconds (or maybe even set some interval). Then the user navigates to another page. Because the messages are not shown anymore (only the top navigation) we never want to revalidate. Then the user navigates to his edit profile page. Here we might not want to show any cached data, but always show the latest data. So we define the query as:
When thinking in this way, we are not really defining the lifetimes of the Conceptually this is a bit different, but I think it would also be my first intuition. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Greeting 👋
Sometimes the same query can be used simultaneously in different places of the application with different fetch and cache strategies.
The most common case is information about current/active user. It can be used in navbar to show user name, on profile form to give user ability to change their data, on actual application scene to build permissions, inside authentication boundaries to protect routes, e.t.c.
It's pretty clear that different cases require different fetch and cache strategies.
In all cases the query is the same. The key difference is how the query is consumed.
react-query provides
cacheTime
andrefetchOnMount
.cacheTime: 0
.refetchOnMount: false
.Looks good so far until you declare the same query with different options at same time in different places. Oooops. Different query consumers shares a single strategy. network-only received cached data.
Possible temporary solutions:
cacheTime
andrefetchOnMount
to query keys. It really works. But it's not a rescue, because brings extra network requests and conceptually is wrong.network-only
. Looks like dirty-hack.Any thoughts on that?
Beta Was this translation helpful? Give feedback.
All reactions