Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

445
Vistas
How can you return status code 404 from angular PageNotFoundComponent?

My understanding is that you can customize the not found page by setting { path: '**', component: PageNotFoundComponent } to RouterMoudle.forRoot(routes).

However it returns http status code 200. How can you return status code 404?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Actually it does not return any status, that page should be bundled with your app, so you won't get any status because there is no request.

There is no point in sending a 404 status as there is no real request when you are changing page in an angular application (as it is a single page application).

Even if your 404 page is located in a lazy-loaded module, you must get the 200 status because the bundle is correctly loaded. Plus, you cannot change the status of a request on client side.

If you use server-side rendering (using angular universal), then your server might be able to send a 404 status on page load, but that would not be while the user browser from one page to another one which does not exist.

about 4 years ago · Santiago Trujillo Denunciar

0

The simple answer to your question is Both Yes and No

it depends upon your scenario. There are two types of Angular Applications.

Client Side(Not Possible in this case)

A normal Angular application executes in the browser, rendering pages in the DOM in response to user actions

These applications adopt the normal approach where the whole application or a part of it(using lazy loading) is loaded inside the client browser. This application contains many small components. Navigation to these components is managed by Angular Router configurations.

Angular assigns paths to these components. Even the PageNotFoundComponent is actually a component and it's placed at the end of all the routes in case the URL doesn't matches any of the defined paths.

Since all of this is happening in client browser in Angular's own environment. So no HTTP request is made to the server.

When there is no request, there is no response

So you can't achieve the desired behavior.

SSR(Server Side Rendering)

There is another type of Angular applications in which the application is rendered from the server in form of static html instead of executing in client browser.

Angular Universal executes on the server, generating static application pages that later get bootstrapped on the client. This means that the application generally renders more quickly, giving users a chance to view the application layout before it becomes fully interactive.

In this case, every page of Angular Application is coming from server as a response of HTTP request.

So

it is possible to also send 404 status code when sending PageNotFoundComponent to the client.

The main reason why you would want to send a 404 error with your component is because search engines don't cache and index the pages with 404 status.

You can have a look at this helpful article which uses the SSR with Angular Universal to achieve this behavior.

about 4 years ago · Santiago Trujillo Denunciar

0

I guess you are mixing two concepts here:

With { path: '**', component: PageNotFoundComponent } you define what component should be shown, if your router can not resolve the given url. The request is handled clientside and angular provides a defined component/page and therefore the request http status code is 200, because there was never a resource which could not be found.

Usually there are only http requests if the 404 page is loaded asynchronously or does contain any extra resources like for example images.

An HTTP 404 on the other side will occur when a webserver can not find a requested resource. So in this case a server handles the response and will provide the correct http status code.

So all in all your use case does not make much sense, because if you want to provide a custom 404 error page with angular, the http response code must be 200.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda