T
TanStack3mo ago
xenial-black

preloading + persist plugin

Hi guys I’ve been exploring TanStack Start and ran into an issue when trying to use the tanstack query persist plugin. When I use useQuery like this, it works as expected — the query is persisted, and if the data already exists, it doesn’t re-fetch from the API.
export const useListOrgs = () =>
useQuery({
queryKey: ['users'],
queryFn: listUsers,
staleTime: Number.POSITIVE_INFINITY,
})
export const useListOrgs = () =>
useQuery({
queryKey: ['users'],
queryFn: listUsers,
staleTime: Number.POSITIVE_INFINITY,
})
but If I try to preload the query using ensureQueryData, it always calls the API again regardless, ignoring the persisted data on the client.
beforeLoad: ({ context }) => {
// this always calls the api again
context.queryClient.ensureQueryData({
queryKey: ['users'],
queryFn: listUsers,
staleTime: Number.POSITIVE_INFINITY,
})
},
beforeLoad: ({ context }) => {
// this always calls the api again
context.queryClient.ensureQueryData({
queryKey: ['users'],
queryFn: listUsers,
staleTime: Number.POSITIVE_INFINITY,
})
},
i expected it dosn't call the api again if the data already exists on client ? right ? also useSuspenseQuery triggers an api call too .. ignoring the persisted data .. only useQuery works fine without preloading
5 Replies
flat-fuchsia
flat-fuchsia3mo ago
have you setup the start-query integration properly?
xenial-black
xenial-blackOP3mo ago
yes i followed the documentation .. its not something with my setup i think this is me misunderstanding how tanstack start works the issue only happens on first render .. which happens on server and it dosn't know anything about my client state and i guess the next route navigations run on client so tanstack start is aware about my client state and it doesn't trigger another api call .. but i'm still confused why this still happens even with { ssr : false } ? even with spa mode enabled .. idk
correct-apricot
correct-apricot3mo ago
can you provide a complete minimal example project ?
correct-apricot
correct-apricot3mo ago
Try to await it
flat-fuchsia
flat-fuchsia3mo ago
pretty sure ensureQueryData always calls the queryFn the reason its not running again on client is precisely because its already there and the fact that you have a staleTime of infinity

Did you find this page helpful?