Skip to content

React Query Adapter always bypasses clientLoader and hits the API #17

Answered by stevensacks
stevensacks asked this question in Q&A
Discussion options

You must be logged in to vote

I made a version that will work with API routes.

export class ReactQueryCache {
  constructor(key: string, gcTime: number) {
    queryClient.setQueryDefaults(key.split(','), {gcTime});
  }

  getItem(key: string) {
    const value = queryClient.getQueryData(key.split(','));

    if (value) {
      return JSON.parse(value as string);
    }

    return undefined;
  }

  async read(key: string, {serverLoader}: ClientLoaderFunctionArgs) {
    if (this.getItem(key)) {
      return this.getItem(key);
    }
    const data = await serverLoader();
    this.setItem(key, data);

    return data;
  }

  removeItem(key: string) {
    return queryClient.removeQueries({
      queryKey: key.split(','),
    

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by stevensacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant