Support custom HTTP Status Code
for Server Components
#53225
-
Previous Proposal
GoalsManage custom Handling
|
Beta Was this translation helpful? Give feedback.
Replies: 31 comments 48 replies
-
@devjiwonchoi |
Beta Was this translation helpful? Give feedback.
-
While I understand the request. It seems there is a misunderstanding about when streaming starts and how that relates to the status code and your code. This proposal can't work because of limitations with streaming, specifically you can't set the status code after the headers have been sent and the stream has started. At the point where the TLDR: because of streaming rendering and the preinitialization (preamble part of the request) in React you can't modify the status / headers, that's also why you can't set cookies / set headers during server components rendering. |
Beta Was this translation helpful? Give feedback.
-
Hi! Thanks for giving this a go! I created the #50383 discussion. The The Server components could also be nested. A fetch call could be triggered in a RSC further down (also consider working with multiple fetch in different server components), then it gets hard to solve how to use the I think it feels more logical to have a |
Beta Was this translation helpful? Give feedback.
-
"Next.js will also return a 404 HTTP status code." Next returns HTTP 404 only when it is raised from root |
Beta Was this translation helpful? Give feedback.
-
Hi there, any updates of this issue? |
Beta Was this translation helpful? Give feedback.
-
404 is just the one example of the codes what can be used to report the NOT FOUND error to the googlebot. For example, what if I need the 410 GONE code? With the next.js sounds impossible at the moment... (And yes, we use it in production, is not just my imagination) What if the user params are wrong, instead of sort=price he passed sort=test and I would like to send the status code 400 for this or even 403, so hacker knows I see his actions |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
We are facing the same issue. We use NextJS in production and are relying on status codes in our (Akamai) CDN to determine what responses can be cached and when to resort to failover. (see https://techdocs.akamai.com/property-mgr/docs/cache-http-err-responses and https://techdocs.akamai.com/property-mgr/docs/site-failover) |
Beta Was this translation helpful? Give feedback.
-
When is |
Beta Was this translation helpful? Give feedback.
-
Hey everyone, I've been thinking about this and to work along with stream, prefetch and set status before streaming like I've updated the proposal, check it out and please share your thoughts. |
Beta Was this translation helpful? Give feedback.
-
Hi, Are any updates and/or input required? Both from development, server, and SEO perspective it's a major requirement. |
Beta Was this translation helpful? Give feedback.
-
Would be great to see something moving forward in this regard, even if it's a plugin that needs to be installed separately. |
Beta Was this translation helpful? Give feedback.
-
I'm missing something or really there is no way to get (using "app dir") the http status code in the "error.js" client-side component page? Currently, I'm stuck on this, which I used to think that should be something simple in any framework/server (at least it is something very trivial in other stacks). My scenario: I have some roles and a check for users that could access a page or not. Sometimes, if the user access a page that does not have permission, I need to throw an error (passing the "403" as a property, for instance) that changes the "error.js" interface to show the specific situation (that is a "Forbidden" page and not a "Server error"). Any thoughts? |
Beta Was this translation helpful? Give feedback.
-
For anyone who's looking for potential solutions our approach could be valuable. So we have a need to respond with 410 status codes when user-generated content was removed from the website or has expired due to inactivity/unpaid accounts. What worked for us is to set the status code via next middleware by making an additional HEAD request to the API and then leveraging CDN (Fastly in our case, but I'm sure there should be similar functionality on others) Not ideal due to second request, but HEAD is lighter and you can optimize with caching per your need. A viable workaround but I'd love this type of thing to be available in a framework that's advertising to be the best SSR solution out there, or at least be way more explicit about this type of breaking/missing features. Middleware code: export async function middleware(request: NextRequest) {
const response = NextResponse.next();
const pathname = request.nextUrl.pathname;
if (*additional filtering based on pathname*) {
try {
const apiResponse = await fetch(`*api-route*`, { method: 'HEAD' });
response.headers.set('X-Status-Code', String(apiResponse.status));
} catch (error) {
logger.error(error);
}
}
return response;
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
}; Fastly config (custom VCL snippet of type "deliver")
|
Beta Was this translation helpful? Give feedback.
-
Hello everyone, I've come across this discussion on the challenge of setting custom status codes in Next.js, and I wanted to share a solution that I've developed, which might be helpful. I've created a proxy middleware named For more details and to try it out, please visit the project repository: NextStatusProxy middleware on Github. Feedback and contributions are most welcome! |
Beta Was this translation helpful? Give feedback.
-
hi there, any update for this issue? |
Beta Was this translation helpful? Give feedback.
-
My main issue with this is that we can't have custom 404 pages. Currently there's only one way to get a proper 404 status is to use But for those who use i18n routes, such as While parsing may work to get the language from the URL in Currently I'm using If we could at least get something like |
Beta Was this translation helpful? Give feedback.
-
In my case, I needed to remove invalid entries from the Google index. To signal their permanent removal, I had to return a custom 410 status code. While I explored other solutions, they weren't ideal for my use case. So here is solution i came out with. create new page called and in the middleware level // middleware.ts
const urlsToBeGone = []; // Populate with URLs to remove or fetch it from cms
const pathname = request.nextUrl.pathname;
if (urlsToBeGone.includes(pathname)) {
return NextResponse.rewrite(new URL('/error', request.url), { status: 410 });
} |
Beta Was this translation helpful? Give feedback.
-
We also need to return a 410 status code for SEO reasons. The middleware-based solutions above won't work for us due to lack of Node.js runtime support (we self-host so technically middleware runs in Node.js, but I digress) |
Beta Was this translation helpful? Give feedback.
-
I would like to add another example where beinging able to set the page status code, without throwing an notFound, would make a lot of sense. On my site I have a search page with searching as you type feature. For sake of SEO, I need to return 404 status when the page has no results. But, since it's a live search and not-found.js does not have access to params and searchParams I cannot do that with Next App Router. (Can I?) Looking foward for this to be in Next.js. The url for search is https://www.bibliaonline.com.br/kjv/busca |
Beta Was this translation helpful? Give feedback.
-
We need too a fine grained management of the HTTP error status codes per page without depending on the middleware |
Beta Was this translation helpful? Give feedback.
-
@leerob can we please remove the answered tag on this discussion? The original question was updated with a solution that should play nicely with the new streaming architecture. |
Beta Was this translation helpful? Give feedback.
-
We definitely need to be able sent a custom status code. It really matters when our Next.js projects need to have a good SEO implementations and deal with the search engine bots properly. |
Beta Was this translation helpful? Give feedback.
-
We really need this feature... |
Beta Was this translation helpful? Give feedback.
-
Any updates on this issue? We need to a way to send different status codes since we want to handle different error pages for unauthorized, server render errors, or BE errors. Can't even pass it through an error object since it will be hidden on production |
Beta Was this translation helpful? Give feedback.
-
Currently, the app router doesn’t allow sending 404 response codes for non-existent dynamic pages, which is essential for customer-facing websites. Adding this feature would significantly improve handling of these scenarios. Can we have this feature soon? |
Beta Was this translation helpful? Give feedback.
-
After spending tons of hours figuring out ways to have correct http status code for 404 and redirects, I tried to find what is actually enabling this Stream rendering for server side. I suggest you also trying to remove / move some stuff away from those Layout files - In our case for locales we used this - https://next-international.vercel.app/docs |
Beta Was this translation helpful? Give feedback.
-
@timneutkens any news? |
Beta Was this translation helpful? Give feedback.
-
I our use case, we need custom dynamic 404 page. Currently, The only workaround is to use client component and refetch data again at client so we can know the 404 reason, which is not optimal. If I can manually set status code at page level, then I can render custom not found page directly in the original page and achieve this. |
Beta Was this translation helpful? Give feedback.
-
To back up on the above and to add a few more uses cases:
|
Beta Was this translation helpful? Give feedback.
While I understand the request. It seems there is a misunderstanding about when streaming starts and how that relates to the status code and your code. This proposal can't work because of limitations with streaming, specifically you can't set the status code after the headers have been sent and the stream has started.
At the point where the
Profile
component in the example runs the headers have already been sent and the stream has been started. This means that you can't change the status code / headers at that point. What we do fornotFound()
/redirect()
when they are called when the stream has started is adding meta tags recommended by Google for these cases.TLDR: because of streaming …