✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Issues with using Sanity and Next.js with getServerSideProps

14 replies
Last updated: Apr 14, 2023
Using Sanity and Next.js using
returns a clean null on
in built (next build && next start) but works fine for dev (next). Any ideas?? (I replaced the client.fetch call with a random fetch(api endpoint) and it worked without issues in build & dev).
Apr 13, 2023, 9:33 PM
How is your client configured?
Apr 13, 2023, 9:40 PM
Using a token and CORS set. Tried both the
and the

const client = createClient({
  projectId: "xxxxxxxx",
  dataset: process.env.NODE_ENV === "production" ? "production" : "dev",
  apiVersion: "2022-04-14", // use current UTC date - see "specifying API version"!
  token: process.env.SANITY_SECRET, // or leave blank for unauthenticated usage
  useCdn: false, // `false` if you want to ensure fresh data
Apr 13, 2023, 9:47 PM
It may be that your client isn’t picking up your token. What have you named your env file?
Apr 13, 2023, 9:58 PM
It seems to be picking up as I can
the token in the build.
Apr 13, 2023, 10:00 PM
Using the HTTP-API of sanity, I get a fetch response of
{ ms: 3, query: '*[0]', result: null }
Apr 13, 2023, 10:22 PM
Do the documents you’re looking for exist in the production dataset?
Apr 13, 2023, 10:23 PM
I am using a get all type query, there are docs in both production and dev datasets. (see above using the HTTP URL api with
resulted in null too
Apr 13, 2023, 10:26 PM
is not a get all query. That’s actually just passing a 0 into your filter. If you’re trying to get all you need to pass an empty filter
or if you’re trying to slice the first document
Apr 13, 2023, 10:28 PM
OK the HTTP-API works to get the data in build (using fetch and axios), but the sanityClient doesn't using either
Apr 13, 2023, 10:52 PM
Ok, that’s good. It means there’s at least data to query. Is there anything in the console?
Apr 13, 2023, 10:54 PM
So in
next start
: the commented out sanityClient code returns a 200 null result from the query, but the axios returns the data, both work in
(dev). Anyway to debug the sanityClient code??
Apr 13, 2023, 10:59 PM
@sanity/client": "^5.4.2",
"next": "latest",
Apr 13, 2023, 11:04 PM
SOLVED: It's a dataset issue:
nextjs assumed production because of the build.. which.. is.. logical. sighh.
user M
you were right 3 messages above 😓
Apr 13, 2023, 11:13 PM
Ah, glad to hear you got it sorted out though! 🙂
Apr 14, 2023, 12:40 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?