Repository navigation
Good practice for server-side data fetching: suspense vs refetch #9725
Replies: 1 comment
|
I would use option 1 for queries you intentionally want to include in SSR. In the Vue adapter, Your SSR-specific override follows the documented pattern: const { suspense } = useQuery({
...myQueryOptions,
enabled: true,
})
onServerPrefetch(() => suspense())If the query depends on authentication or required parameters, preserve those prerequisites rather than enabling it unconditionally. Queries intended only for the browser can retain the client-only default. Also hydrate the server query state on the client and choose an appropriate Option 2 is valid when you specifically want a manual fetch, but it is not equivalent to waiting for an enabled query while respecting cache freshness. References: Vue SSR guide, Vue adapter implementation. |
Uh oh!
There was an error while loading. Please reload this page.
Hey everyone,
We are using tanstack/query in a nuxt project. My question will be related to the difference between the
refetchandsuspensefunctions in a specific setting, but let me give some context:For suspending a network query on the server-side, the docs advice using:
However, I noticed that
useQuery, as long as it is enabled, will always trigger the queryFn on the server-side; meaning if we don't suspend it, will just cause us to have a bunch of floating network requests.So, I wanted to add a global
enabledoption for the vueQuery instance toenabled: import.meta.client. In this approach, we fix the unnecessary network calls. Though for the occasions where we do want the suspense behaviour, we have two options:Now, I'm a bit torn between these two options, and I feel like one thing I'm missing is the difference between
suspenseandrefetch. So, in this context, what do you guys think would be a better way to go?Thanks!
All reactions