3

I have a custom 404 page for my Nextjs app. I wanted to try the build project locally before deploying it to server. I used the "serve" package to host the project locally. The project loads fine but when I tried to navigate to a page that doesn't exist, the app uses the default 404 page and not the customized one. I just want to ask if there are needed steps to configure in order to run the custom 404 page from Next.js or if it is possible to load the custom 404 page locally.

In the browser:

Browser

404.tsx file:

404.tsx file

next.config file:

module.exports = {
  trailingSlash: true,
  webpack: (config, options) => {
    config.module.rules.push({
      test: /\.(html)$/,
      use: {
        loader: 'html-loader',
      },
    })
    return config
  },
}

404.tsx

export default function FourOhFour() {
  return <>
    <h1>404 - Page Not Found - Test 404 Page</h1>
  </>
}
1
  • This also can happen if you have set fallback: true in getStaticPaths, although the docs are a bit misleading here, IMO: data-fetching/get-static-paths#fallback-true. Commented Nov 8, 2022 at 10:09

2 Answers 2

3

you create in pages/404.js (or .tsx) You don't need to change config, the page will be rendered in case of 404.

Maybe you have cache problem (delete your .next folder and try to build again)

P.S. probably you will need to add your "pages/404.js" to tsconfig.json

Sign up to request clarification or add additional context in comments.

1 Comment

Delete .next folder and build again works for me. :)
2

The "serve" package does not allow you to customise the 404 page (from the doc). The page to serve in case of 404 is a server configuration, for instance, on nginx you can configure it with the directive error_page 404 /404.html;.

You need to look in the your web server doc how to configure 404 pages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.