Home / Snippets / How to Create Custom Error Pages for Next Js
How to Create Custom Error Pages for Next Js cover

How to Create Custom Error Pages for Next Js

663

2 years ago

0 comments

To create custom error pages such as 404 and 500 in your Next.js web application, you can create a custom file named "404.js" as well as "500.js" inside the "pages/" directory.
  • 404 => 404.js
  • 500 => 500.js
By having these custom files, Next.js will automatically render and show this page when 404 / 500 errors occur within your web application.

Next.js 404 Code Example


For the 404 error codes, you can have the "404.js" page export like the following. Do note that you can customize it however you want it to be.
// pages/404.js

export default function Custom404() {
  return <h1>404 - Page Not Found</h1>
}

Next.js 500 Code Example


For 500 error codes, it's the same as 404 and you can refer the code example below.
// pages/500.js

export default function Custom500() {
  return <h1>500 - Server-side error occurred</h1>
}
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this