Ask Your Question
2

What steps should I follow to set up custom error pages for 404 and 500 errors on my Next.js application?

asked 2021-10-03 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-01-18 21:00:00 +0000

woof gravatar image

Here are the steps to set up custom error pages for 404 and 500 errors on your Next.js application:

  1. Create the pages for the custom error pages:
    • For 404 errors, create a file called 404.js in the pages directory.
    • For 500 errors, create a file called 500.js in the pages directory.
  2. In each file, add the following code:

    function CustomErrorPage() {
     return (
       <div>
         {/* Add your custom error message and styles here */}
       </div>
     )
    }
    
    export default CustomErrorPage
    
  3. In your Next.js configuration file (usually next.config.js), add the following code:

    module.exports = {
     // ...
     webpack: (config, { isServer }) => {
       if (isServer) {
         require('./scripts/generate-sitemap') // Your custom sitemap file
         const originalEntry = config.entry.bind(config)
         config.entry = () => originalEntry().then(entry => {
           entry['./scripts/generate-sitemap'] = './scripts/generate-sitemap.js'
           return entry
         })
       }
    
       return config
     },
     // ...
     eslint: {
       ignoreDuringBuilds: true,
     },
     serverRuntimeConfig: {
       // Will only be available on the server side
       mySecret: 'secretsauce',
       SECOND_SECRET: process.env.SECOND_SECRET, // Pass through env variables
     },
     publicRuntimeConfig: {
       // Will be available on both server and client
       staticFolder: '/public',
     },
     distDir: 'build',
     webpackDevMiddleware: (config) => {
       return config;
     },
    }
    
  4. Add the following code to your next.config.js file so that Next.js knows to use the custom error pages:

    module.exports = {
     // ...
     exportTrailingSlash: true,
     async rewrites() {
       return [
         {
           source: '/404',
           destination: '/404',
         },
         {
           source: '/500',
           destination: '/500',
         },
       ]
     },
     async redirects() {
       return [
         {
           source: '/404',
           destination: '/404',
           permanent: true, // true means a 301 redirect
         },
         {
           source: '/500',
           destination: '/500',
           permanent: true,
         },
       ]
     },
     // ...
    }
    
  5. Restart your server to see the changes take effect.

That's it! Now your Next.js application will show custom error pages for 404 and 500 errors.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-03 11:00:00 +0000

Seen: 11 times

Last updated: Jan 18 '22