Skip to content

Next.js - initialData from server, staleTime and queryKey #3210

Answered by TkDodo
kdoroszewicz asked this question in Q&A
Discussion options

You must be logged in to vote

I tried to solve this by setting staleTime on the query but in that case currentCategory changes don't trigger fetching new promoted products because staleTime hasn't passed yet. The change in queryKey only works again after stale time passes.

changes in query key will switch to that cache entry, and if that entry has fresh data, that one will be taken without refetch, so that is expected. if you change to a category that does not yet have data, you should get a hard loading state.

please keep in mind that when providing initialData, it will be passed to all query keys:

useQuery(
  [todo, id],
  () => fetchTodo(id),
  { initialData: { title: 'my todo' } }
)

if I change from id 1 to id …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kdoroszewicz
Comment options

@chinanderm
Comment options

Answer selected by kdoroszewicz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants