Gatsby SSR page, 404 response not working as expected

Hi, I’m currently trying to trigger a 404 page from a Gatsby SSR page template. Locally this setup appears to work as expected, but I don’t see the behaviour I’m expecting once deployed to Netlify.

Here’s an example page which should return 404 https://deploy-preview-21--100-gallery.netlify.app/post/?name=foobar

and here’s the key part in my code…

/* handle record not found as 404 */
    if (!data.length) {
      return {
        status: 404,
        headers: {},
        props: {},
      }
    }

… which locally results in…

Am I missing a key piece of config or plugin somewhere?

thanks

Rich

This is the response I get locally with a built version - note the Status comes back 404.

The UI is as desired - showing 404 content, but the http response status is 200 when deployed to Netlify.

Thanks for reaching out!

This definitely isn’t intended behavior; a minimal reproduction would be the best next step for getting to the bottom of why the 200 is being served on Netlify, as including the 404 status on an SSR page should be enough to have Netlify return that status as well, e.g:

const SSRPage = () => (
  <main>
    <h1>404 Test</h1>
  </main>
)

export const Head = () => <Seo title="SSR Page" />

export default SSRPage

export async function getServerData() {
    return {
        status: 404,
        headers: {},
        props: {}
    }
}

https://master--astonishing-gaufre-3c2fc9.netlify.app/second-ssr/

Hey Marcus, thanks for the reply.

Here’s my minimum repro repo - I’ve stripped the src right back but kept all dependencies as they are, note the Gatsby version is 4.25.7.

And here’s the deployed site - the 404 behaviour persists.

https://main--zingy-concha-094b69.netlify.app/

thanks

Rich

So I tried creating a fresh repo with latest Gatsby and a completely new Netlify app then manually lifted across all my existing config & components and now it’s working as expected :tada:

Can only assume there must have been something amiss between Gatsby V4 and the older Netlify app/plugins (?)

Hi Rich,

Thanks for following up and letting us know! It sounds likely, but we wouldn’t be able to confirm without continuing to dig deeper. I’m really glad that you’ve got things up and running - don’t hesitate to get back in touch if anything else comes up.