QueryClient support defaultOptions queryKey for set token to queryKey? #9303
Answered
by
976520
vaynevayne
asked this question in
General
-
I need to put the token in all the queryKey, is there anything like the middleware of swr? |
Beta Was this translation helpful? Give feedback.
Answered by
976520
Jun 28, 2025
Replies: 1 comment 3 replies
-
Is this usage correct? import { useQueryClient, hashKey } from '@tanstack/react-query'
const queryClient = new QueryClient({
defaultOptions: {
queries: {
gcTime: 1000 * 60 * 60 * 24, // 24 hours
queryKeyHashFn: queryKey => {
const { token } = useLocalStore.getState()
return hashKey(queryKey.concat(token))
},
},
},
}) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@vaynevayne
If I understood correctly, you’re including the token in the queryKey to prevent cached data from a previous user being shown after a user switch — basically to isolate or invalidate queries on user change. That makes sense. 😁
That said, adding the token to the queryKey (as in your example using a custom
queryKeyHashFn
) might lead to some subtle cache duplication issues, since the token gets hashed in and may not be obvious from the key structure. If you include the token in the queryKey, there's a risk that the auth token could get exposed externally through browser devtools, error reporting tools, etc.queryKey is an identifier that defines "what data to fetch." But the toke…