Issues with using Sanity and Next.js with getServerSideProps
14 replies
Last updated: Apr 14, 2023
O
Using Sanity and Next.js using
getServerSidePropsreturns a clean null on
client.fetch(...)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
R
How is your client configured?
Apr 13, 2023, 9:40 PM
O
Using a token and CORS set. Tried both the
@sanity/clientand the
next-sanity
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
R
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
O
It seems to be picking up as I can
console.logthe token in the build.
Apr 13, 2023, 10:00 PM
O
Using the HTTP-API of sanity, I get a fetch response of
{ ms: 3, query: '*[0]', result: null }
Apr 13, 2023, 10:22 PM
R
Do the documents you’re looking for exist in the production dataset?
Apr 13, 2023, 10:23 PM
O
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
fetchresulted in null too
Apr 13, 2023, 10:26 PM
R
*[0]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
*[][0]
Apr 13, 2023, 10:28 PM
O
OK the HTTP-API works to get the data in build (using fetch and axios), but the sanityClient doesn't using either
next-sanityor
@sanity/client
Apr 13, 2023, 10:52 PM
R
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
O
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
next(dev). Anyway to debug the sanityClient code??
Apr 13, 2023, 10:59 PM
O
Using
@sanity/client": "^5.4.2",and
"next": "latest",
Apr 13, 2023, 11:04 PM
O
SOLVED: It's a dataset issue:
Sorry
process.env.NODE_ENVnextjs assumed production because of the build.. which.. is.. logical. sighh.
Sorry
user M
you were right 3 messages above 😓Apr 13, 2023, 11:13 PM
R
Ah, glad to hear you got it sorted out though! 🙂
Apr 14, 2023, 12:40 AM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.