Returning 500 or 503 Error Response Status Code from Layout or Page #52378
Replies: 9 comments 1 reply
-
I'm also interested in having this feature. Created this discussion a while ago #50383 |
Beta Was this translation helpful? Give feedback.
-
Is there a way to achieve this with Page Router? I have seen that |
Beta Was this translation helpful? Give feedback.
-
I'm also curious as well. Would love a function like |
Beta Was this translation helpful? Give feedback.
-
We are humans, we need this |
Beta Was this translation helpful? Give feedback.
-
👋 Any progress now? I also need to return a 500 page to the user when the API returns an error |
Beta Was this translation helpful? Give feedback.
-
We need this |
Beta Was this translation helpful? Give feedback.
-
Any update on that ? |
Beta Was this translation helpful? Give feedback.
-
Not saying this solves the original question, nor that it's pretty, but it covered my requirement sufficient enough. Hope it helps someone. I needed to display my site in maintenance mode when a flag was toggled in the CMS. I managed to display my page with:
I added a fetch to our cms in my Middleware.ts, if maintenance mode was enabled:
In my layout.tsx I re-fetched the maintenance mode flag from the CMS and showed my maintenance component instead of |
Beta Was this translation helpful? Give feedback.
-
I've tried this and I correctly get a 500 server error status code. Maybe this problem got fixed at some point? My Next version is my-app/src/app/page.tsx: export default function Page() {
throw new Error("ERROR!");
} my-app/src/app/error.tsx: "use client";
export default function Error() {
return <div>Error</div>;
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The
error.js
functionality is great for customizing the visual display when errors occur at different levels of the app:https://nextjs.org/docs/app/building-your-application/routing/error-handling
However, as far as I'm aware, it always returns a 200 response code. This might be ok for small contained errors, but for substantial errors, I would like to return an error status code to the client from a
layout.js
orpage.js
.For example, if I am retrieving data from an API in
page.js
and the API returns an error or is unavailable, then I cannot render my page, so I would like to return a 500 or 503 error.This is particularly important for search engines, as returning a 200 status for an error page will cache the error page, rather than indicate to the crawler there's an error and to try again later.
Functionality similar to
notFound()
would be ideal, where it can be called from a layout or page triggers the nearest error page and sets the response code e.g.serverError()
serverUnavailable()
, orerror(code)
Like
notFound()
uses'not-found.js
, this may also need a corresponding server component file likeserver-error.js
, as currently the defaulterror.js
must be a client component, which may limiting.Beta Was this translation helpful? Give feedback.
All reactions