You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Should enable users to define an error component without having to worry about creating an entire custom error page.
Non-Goals
Should NOT have any advanced or fancy features to define styling or layouting.
Should NOT influence routing in any way.
Background
The Pages router has a default error component that can be imported via:
importErrorfrom"next/error"
This makes it very easy to create custom error pages (_error.tsx) that make use of the default Next.js styling and logic of the error page, but allows you to add additional functionality and side-effects like error reporting.
It would be neat to also have a default Error component that can be used in the App Router, so that users can add logic to their error.tsx and global-error.tsx components without having to worry about styling or particular logic.
The current default error component as exported from "next/error" is hard to use in the app router as of today because it has a required statusCode property. This required statusCode property comes from the fact that the pages router default error component is supposed to be used in combination with the _error page getInitialProps function which returns a status code. There is no place where you could derive a status code from in the App Router.
An interesting detail to mention would be that currently if no error or global-error components are defined in the App Router, Next.js will display the "next/error" Error component (without using a statusCode property).
Proposal
The easiest and in my humble opinion preferred solution would be to simply make the statusCode property of the current Error component optional. This would allow us to use the current next/error component in both routers without having to worry about additional functionality or docs. Making the statusCode property optional should be backwards compatible. This is what Next.js is currently doing as a fallback anyhow.
Alternative solutions:
Create a different default error component for the App router.
Provide some sort of onError export in error and global-error App Router components:
// global-error.tsx'use client'// Note that no default export would be required.exportfunctiononError(error: Error): void{// Do your error reporting here.}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Non-Goals
Background
The Pages router has a default error component that can be imported via:
This makes it very easy to create custom error pages (
_error.tsx) that make use of the default Next.js styling and logic of the error page, but allows you to add additional functionality and side-effects like error reporting.It would be neat to also have a default
Errorcomponent that can be used in the App Router, so that users can add logic to theirerror.tsxandglobal-error.tsxcomponents without having to worry about styling or particular logic.The current default error component as exported from
"next/error"is hard to use in the app router as of today because it has a requiredstatusCodeproperty. This requiredstatusCodeproperty comes from the fact that the pages router default error component is supposed to be used in combination with the_errorpagegetInitialPropsfunction which returns a status code. There is no place where you could derive a status code from in the App Router.An interesting detail to mention would be that currently if no
errororglobal-errorcomponents are defined in the App Router, Next.js will display the"next/error"Error component (without using astatusCodeproperty).Proposal
The easiest and in my humble opinion preferred solution would be to simply make the
statusCodeproperty of the currentErrorcomponent optional. This would allow us to use the currentnext/errorcomponent in both routers without having to worry about additional functionality or docs. Making thestatusCodeproperty optional should be backwards compatible. This is what Next.js is currently doing as a fallback anyhow.Alternative solutions:
onErrorexport inerrorandglobal-errorApp Router components:Beta Was this translation helpful? Give feedback.
All reactions