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
A
I am trying to use sanity with Next.js, like this:
But getting this error:
Why isnt a plain object returned?
export const getServerSideProps: GetServerSideProps<Page> = async () => { return { props: await client.fetch<Page>("[_type == 'page' && title == 'About' ]") } }
Error serializing props returned from `getServerSideProps` in "/". Reason: Props must be returned as a plain object from getServerSideProps: `{ props: { ... } }`.
Apr 28, 2020, 6:11 PM
export const getServerSideProps: GetServerSideProps<Page> = async () => { return { props: { pageDate: await client.fetch<Page>("[_type == 'page' && title == 'About' ]") }} }
Apr 28, 2020, 6:13 PM
export const getServerSideProps: GetServerSideProps<Page> = async () => { return { props: await client.fetch<Page>("[_type == 'page' && title == 'About' ][0]") } }
Apr 28, 2020, 6:13 PM
A
didn’t make any difference
Apr 28, 2020, 6:14 PM
A
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
A
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
A
it prints
YAY:false
Apr 28, 2020, 6:18 PM
A
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
A
yes
Apr 28, 2020, 6:19 PM
A
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
A
* ?
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
A
there we go 🙂
Apr 28, 2020, 6:21 PM
A
Thanks ❤️
Apr 28, 2020, 6:21 PM
*[_type == 'page' && title == 'About' ]
[_type == 'page' && title == 'About' ]
falseapparently
Apr 28, 2020, 6:21 PM
A
ok, so
*represents the universe, and then you restrict it?
Apr 28, 2020, 6:22 PM
A
nice
Apr 28, 2020, 6:22 PM
A
is my understanding correct that Sanity itself is schema-less?
Apr 28, 2020, 6:22 PM
A
but Studio has a schema?
Apr 28, 2020, 6:22 PM
A
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
A
is
_typerequired?
Apr 28, 2020, 6:23 PM
A
I am trying to set up a TypeScript-oriented workflow but so far it has been very tough
Apr 28, 2020, 6:24 PM
You might want to check out https://github.com/danielroe/sanity-typed-queries
Apr 28, 2020, 6:24 PM
A
I have tried it
Apr 28, 2020, 6:24 PM
A
it works in and of itself
Apr 28, 2020, 6:25 PM
A
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
A
Ideally, I’d like to generate the sanity schema from the TypeScript definitions
Apr 28, 2020, 6:26 PM
A
its totally doable, we have done similar things for other purposes
Apr 28, 2020, 6:26 PM
A
but its a bit of work
Apr 28, 2020, 6:26 PM
A
anyway, thanks for the unblock 🙂
Apr 28, 2020, 6:28 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.