Caching Sanity client-side requests in Next.js without repeat calls

1 replies
Last updated: Jun 22, 2021
Hey folks, this isn't strictly a Nextjs thing but since I'm working in Next for the project I figured I'd ask it here anyway.
I have a page which makes requests (from the clientside) to Sanity based on user selections. Currently I'm just using the
next-sanity
SanityClient to make the requests with Groq queries. However I'd like to consider using a library with some caching built in so I could avoid making repeat calls to retrieve the same data. I'm not familiar with many of the options out there that would work well in this clientside scenario, so I thought I'd field it to you folks for some suggestions.
Oh and I'm not looking for cache persistence between page loads/users, just while the user stays on the page and does various actions during that page's lifetime before a postback. It would be cool if it worked and held the cache between Next
Link
page transitions, but that would just be a bonus.
AI Update

Great question! For client-side caching of Sanity queries in React/Next.js, React Query (now TanStack Query) is an excellent choice and commonly recommended in the Sanity community.

Here's why React Query works well for your use case:

Automatic Request Deduplication & Caching React Query will automatically cache your Sanity queries based on query keys. If you make the same query multiple times (like when a user toggles between selections), it'll serve the cached data instantly without hitting Sanity again.

Basic Implementation Example

import { useQuery } from '@tanstack/react-query'
import { client } from './sanityClient'

function useProducts(category, filters) {
  return useQuery({
    queryKey: ['products', category, filters], // Cache key based on user selections
    queryFn: () => client.fetch(YOUR_GROQ_QUERY, { category, filters })
  })
}

Cache Persistence Across Next.js Page Transitions React Query actually does persist cache across Next.js <Link> transitions by default! Since Next.js doesn't unmount the entire React tree during client-side navigation, React Query's cache stays alive. This gives you that bonus feature you mentioned.

Other Options to Consider

  • SWR (by Vercel) - Similar to React Query but lighter weight. Also works great with Sanity
  • Plain React state management - If your needs are simple, useState + a custom cache object could work

Important Note The sanityFetch helper from next-sanity you might have seen is specifically for server-side caching in Next.js App Router. Since you're making client-side requests based on user interactions, React Query is the right tool for the job.

React Query also gives you nice loading states, error handling, background refetching, and tons of other features that make building interactive UIs much easier. Definitely worth checking out!

Show original thread
1 reply
Hey! React Query https://react-query.tanstack.com/ could help you with this. Just pass the user selections as a query key, and it will cache the requests for you πŸŽ‰

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?