Sanity Pioneers: Get early access to betas, extra AI credits, and a direct line to the engineering team. Apply now

getServerSideProps returning null instead of props object with Sanity query

39 repliesLast updated: Nov 29, 2025

Why isn’t my [slug].js page displaying when going to the page?I keep getting this error

error - Error: Error serializing props returned from `getServerSideProps` in "/[slug]".
Reason: Props must be returned as a plain object from getServerSideProps: `{ props: { ... } }` (received: `[object Null]`).
This is what my getServerSideProps looks like

export const getServerSideProps = async function (context) {
    const { slug = '' } = context.query;
  
    const data = await client.fetch(`*[_type == "hikes" && slug.current == $slug][0] `, { slug });
    return { props: data };
};
I’ve used this ^ structure on a different slug page and it works
AI Update

The issue you're encountering is that your GROQ query is returning null when no document matches, but Next.js expects getServerSideProps to return an object with a props key containing a plain object, not null.

The error message is clear: you're returning { props: data } where data is null, which means you're effectively returning { props: null } instead of { props: { ... } } (a plain object).

The fix is simple - you need to handle the case where your query returns no results:

export const getServerSideProps = async function (context) {
    const { slug = '' } = context.query;
  
    const data = await client.fetch(
        `*[_type == "hikes" && slug.current == $slug][0]`, 
        { slug }
    );
    
    // Handle null/undefined data
    if (!data) {
        return {
            notFound: true, // Shows 404 page
        };
    }
    
    return { 
        props: { data } // Wrap data in an object
    };
};

Why this happens:

Additional improvement:

Consider wrapping your data in a named property for clarity:

return { 
    props: { hike: data }
};

Then in your component:

export default function HikePage({ hike }) {
    // Use hike data
}

The reason your other slug page works is likely because that query always returns data, so you never hit the null case. Once you add the null check with notFound: true, users will see a proper 404 page when navigating to a slug that doesn't exist in your Sanity dataset.

Show original thread
39 replies

Was this answer helpful?

Sanity – Build the way you think, not the way your CMS thinks

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.

Related contributions