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

Discussion on using Sanity with Next.js, including an error message and troubleshooting, and a brief tangent on TypeScript support.

43 replies
Last updated: Apr 28, 2020
I am trying to use sanity with Next.js, like this:
export const getServerSideProps: GetServerSideProps<Page> = async () => {
    return { props: await client.fetch<Page>("[_type == 'page' && title == 'About' ]") }
}
But getting this error:

Error serializing props returned from `getServerSideProps` in "/". Reason: Props must be returned as a plain object from getServerSideProps: `{ props: { ... } }`.
Why isnt a plain object returned?
Apr 28, 2020, 6:11 PM
export const getServerSideProps: GetServerSideProps<Page> = async () => {
    return { props: { pageDate: await client.fetch<Page>("[_type == 'page' && title == 'About' ]") }}
}
Because the fetch returns an array of documents (in this case probably just one)
Apr 28, 2020, 6:13 PM
export const getServerSideProps: GetServerSideProps<Page> = async () => {
    return { props: await client.fetch<Page>("[_type == 'page' && title == 'About' ][0]") }
}
could also work
Apr 28, 2020, 6:13 PM
didn’t make any difference
Apr 28, 2020, 6:14 PM
an array is still a plain object, so if that was the problem, we should have seen a different error message
Apr 28, 2020, 6:14 PM
but it tries to serialize the keys in “a plain object" as props for the react component? That doesn't work with an array where the keys would be numbers from the index?
Apr 28, 2020, 6:17 PM
    const aboutPage = await client.fetch<Page>("[_type == 'page' && title == 'About' ][0]")
    console.log("YAY:" + JSON.stringify(aboutPage))
    return { props: aboutPage }
Apr 28, 2020, 6:18 PM
it prints
YAY:false
Apr 28, 2020, 6:18 PM
so the fetch returned just
false
Apr 28, 2020, 6:19 PM
Anywho. Do you get anything logged from
export const getServerSideProps: GetServerSideProps<Page> = async () => {
  const data = await client.fetch<Page>("[_type == 'page' && title == 'About' ]")
  console.log(data)
    return { props: { data }}
}
?
Apr 28, 2020, 6:19 PM
the fetch returns
false
?
Apr 28, 2020, 6:19 PM
yes
Apr 28, 2020, 6:19 PM
could it be that the sanity client doesn’t like to run in the server side environment of Next ?
Apr 28, 2020, 6:19 PM
No. We're doing it on sanity.io
Apr 28, 2020, 6:20 PM
Ah
Apr 28, 2020, 6:20 PM
we're missing the *
Apr 28, 2020, 6:20 PM
* ?
Apr 28, 2020, 6:20 PM
export const getServerSideProps: GetServerSideProps<Page> = async () => {
    return { props: { pageDate: await client.fetch<Page>("*[_type == 'page' && title == 'About' ]") }}
}

Apr 28, 2020, 6:20 PM
there we go 🙂
Apr 28, 2020, 6:21 PM
Thanks ❤️
Apr 28, 2020, 6:21 PM
*[_type == 'page' && title == 'About' ]
vs

[_type == 'page' && title == 'About' ]
So we tried to filter “nothing”, and that returns
false
 apparently
Apr 28, 2020, 6:21 PM
ok, so
*
represents the universe, and then you restrict it?
Apr 28, 2020, 6:22 PM
correct
Apr 28, 2020, 6:22 PM
nice
Apr 28, 2020, 6:22 PM
is my understanding correct that Sanity itself is schema-less?
Apr 28, 2020, 6:22 PM
but Studio has a schema?
Apr 28, 2020, 6:22 PM
The datastore is schemaless, the studio (and GraphQL) have schemas
Apr 28, 2020, 6:23 PM
thanks, sorry for going off-topic in the same thread
Apr 28, 2020, 6:23 PM
so you can upload any JSON document (as long as you give it a
_type
) and it will be queryable with GROQ
Apr 28, 2020, 6:23 PM
is
_type
required?
Apr 28, 2020, 6:23 PM
yup - which makes it more “schemaless” I guess
Apr 28, 2020, 6:23 PM
I am trying to set up a TypeScript-oriented workflow but so far it has been very tough
Apr 28, 2020, 6:24 PM
Apr 28, 2020, 6:24 PM
I have tried it
Apr 28, 2020, 6:24 PM
aha
Apr 28, 2020, 6:24 PM
it works in and of itself
Apr 28, 2020, 6:25 PM
Better TypeScript support are coming, slowly but surely.
Apr 28, 2020, 6:25 PM
but since Next.js doesn’t support reaching out to code outside of the root folder, it falls appart
Apr 28, 2020, 6:25 PM
Ideally, I’d like to generate the sanity schema from the TypeScript definitions
Apr 28, 2020, 6:26 PM
You and many others I suspect
Apr 28, 2020, 6:26 PM
its totally doable, we have done similar things for other purposes
Apr 28, 2020, 6:26 PM
but its a bit of work
Apr 28, 2020, 6:26 PM
anyway, thanks for the unblock 🙂
Apr 28, 2020, 6:28 PM
👍
Apr 28, 2020, 6:31 PM

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?