angulartypescripthttp-status-code-404angular19

How can I change the response status from a component to the server (SSR) in Angular v19


I am working on an Angular v19 project with SSR enabled for better SEO. I have two components:

If the entity does not exist, I want to redirect the user to the 404 page and return a 404 HTTP status instead of 200.

Currently, when I make a curl request to the URL of a non-existing entity, I receive the correct 404 page content, but the HTTP status remains 200 instead of 404.

Is there a way to modify the HTTP status code to 404 from inside my 404-page-not-found component when running Angular with SSR? If not, what would be the correct approach to achieve this behavior?

Any help would be appreciated!


Solution

  • After several attempts, I have concluded the following points:

    Pros & Cons

    āœ… Advantage: A proper 404 status is returned.

    āŒ Drawback: The server.ts file requires a knownEntitiesList, or an API call to verify existence, which can introduce additional latency. If the entity exists, this approach results in two API calls instead of one (one server-side check and another browser-side request).

    An example is available in StackBlitz

    Know with this method using curl you get: