Search code examples
javascriptreactjsnext.jsserver-side-renderingproduction

Next js 404 page doesn't work. What could be the problem?


When I try to go on a non-existing route, always load the home page but the URL doesn't change. I expected to receive a 404 page.

I create a custom error page pages/_error.js

import Page404 from './404';

function Error({ statusCode }) {
  return statusCode ? (
    <p>{`An error ${statusCode} occurred on server`}</p>
  ) : (
    <Page404 />
  );
}

Error.getInitialProps = ({ res, err }) => {
  const statusCode = res ? res.statusCode : err ? err.statusCode : 404;
  return { statusCode };
};

export default Error;

Tell me please, what I am doing wrong?


Solution

  • I found a solution. In nginx.conf I find following line try_files $uri $uri/ /index.html =404; and change /index.html to /404/index.html it's helped. But i dont know if this a good solution.