Gatsby: 404 Flashes Before Showing Correct Page

Created on 10 Oct 2019  路  4Comments  路  Source: gatsbyjs/gatsby

I'm trying to have the following route render a page: /tenants/:name.

I wrote this code in gatsby-node.js:

exports.onCreatePage = ({ page, actions }) => {
  const { createPage } = actions;
  if (page.path.includes("tenants")) {
    page.matchPath = `/tenants/:name`;
    createPage(page);
  }
};

This works, however, when I build on Netlify, it returns a 404 error in the console. The page still loads the tenants page, but the 404 page flashes first.

This is not broken in development, just when built.

Most helpful comment

I solved this by adding a check in my 404.js for the window. If it's undefined, I don't render the 404 page.

All 4 comments

I solved this by adding a check in my 404.js for the window. If it's undefined, I don't render the 404 page.

Hi,
I just wanted to say thank you for posting this issue. I was struggling with this, until I found this.

@jevinsidhu can you please share your check code in the 404.js? It's not clear to me what are you checking.

@ilyador Here's what my 404.js looks like:

const ErrorPage = () => {
  const browser = typeof window !== "undefined" && window;

  return (
    <>
      {browser && (
        <>
            <Heading>Page Not Found</Heading>
            <HomeButton to="/">Go Back to Home</HomeButton>
        </>
      )}
    </>
  );
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ferMartz picture ferMartz  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

kalinchernev picture kalinchernev  路  3Comments

timbrandin picture timbrandin  路  3Comments

magicly picture magicly  路  3Comments