
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThis is a classic Next.js serialization issue! The error "Cannot stringify a function image" occurs because you're likely passing your Portable Text serializers/components through Next.js data-fetching methods like getStaticProps or getServerSideProps, which can only return JSON-serializable data.
The Problem:
When you navigate within your app (client-side routing), the serializers work fine because they're already loaded in the browser as JavaScript functions. But when you access the page directly via URL (server-side rendering), Next.js tries to serialize the props returned from getStaticProps/getServerSideProps, and functions (like your image serializer) cannot be serialized to JSON.
The Solution:
Move your Portable Text component configuration to the client-side component rather than passing it through your data-fetching functions. Here's how:
❌ Don't do this:
// pages/noticias/[slug].js
export async function getStaticProps({ params }) {
const post = await client.fetch(query, { slug: params.slug })
return {
props: {
post,
components: { // ❌ Can't serialize functions!
types: {
image: MyImageComponent
}
}
}
}
}✅ Do this instead:
// pages/noticias/[slug].js
import { PortableText } from '@portabletext/react'
import imageUrlBuilder from '@sanity/image-url'
const builder = imageUrlBuilder(client)
// Define components in the component file, not in getStaticProps
const components = {
types: {
image: ({ value }) => (
<img
src={builder.image(value).url()}
alt={value.alt || ''}
/>
)
}
}
export default function NoticiaPage({ post }) {
return (
<article>
<PortableText
value={post.body}
components={components} // ✅ Defined client-side
/>
</article>
)
}
export async function getStaticProps({ params }) {
const post = await client.fetch(query, { slug: params.slug })
return {
props: {
post // ✅ Only serializable data
}
}
}Key Points:
getStaticProps/getServerSidePropsIf you're using the older block-content-to-react or custom serializers, the same principle applies: keep the serializer functions in your component code, not in your data-fetching returns.
This is a Next.js limitation - the devalue library they use for serialization cannot handle functions, which is why you see this error specifically on direct page loads but not on client-side navigation.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store