#preloading + persist plugin

7 messages · Page 1 of 1 (latest)

gilded bolt
#

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,
  })

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,
    })
  },

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

stuck iris
#

have you setup the start-query integration properly?

gilded bolt
# stuck iris have you setup the start-query integration properly?

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

wintry siren
#

can you provide a complete minimal example project ?

tight nebula
#

Try to await it

stuck iris
#

pretty sure ensureQueryData always calls the queryFn

#

the reason its not running again on client is precisely because its already there