ReasonJun

Next.js : Error Handling 본문

Frontend/Next.js

Next.js : Error Handling

ReasonJun 2023. 6. 21. 13:40
728x90

Error handling is an important aspect of any web application, especially when working with Next.js. Next.js provides different ways to handle errors in development, server-side, and client-side scenarios. In this article, we will explore some of the best practices for error handling in Next.js 13.

 

In development mode, Next.js shows an overlay that covers the webpage when there is a runtime error. This overlay helps developers to quickly identify and fix the error. The overlay will disappear once the error is resolved.

 

For server-side errors, Next.js provides a default 500 page that can be customized by creating a pages/500.js file. This page will be rendered when there is an error on the server that cannot be handled by the application logic. The 500 page should not expose any sensitive information to the app user, but rather provide a generic message and a way to contact support.

 

For client-side errors, Next.js recommends using React Error Boundaries, which are class components that can catch JavaScript errors in their child components and render a fallback UI. Error Boundaries can also log error information and provide a way to reset the application state. To use Error Boundaries in Next.js, we need to create an ErrorBoundary component and wrap it around the Component prop in pages/_app.js file.

 

https://nextjs.org/docs/app/building-your-application/routing/error-handling

 

Routing: Error Handling | Next.js

Using App Router Features available in /app

nextjs.org

https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary

 

Component – React

The library for web and native user interfaces

react.dev

 

728x90
Comments