最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

server side rendering - How to respond with 404 status on not found page on Angular? - Stack Overflow

programmeradmin4浏览0评论

I have a "not found" component:

{
    path: '**',
    loadComponent: () =>
      import('./pages/not-found-pageponent').then(
        (c) => c.PageNotFoundComponent,
      ),
}

How can I send the 404 status in the response, so Google doesn't show a soft 404 problem on my app?

I have a "not found" component:

{
    path: '**',
    loadComponent: () =>
      import('./pages/not-found-pageponent').then(
        (c) => c.PageNotFoundComponent,
      ),
}

How can I send the 404 status in the response, so Google doesn't show a soft 404 problem on my app?

Share Improve this question edited Mar 26 at 18:20 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 26 at 17:40 Sebastián RojasSebastián Rojas 2,8901 gold badge28 silver badges35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Since Angular version 19, you can easily change the status code only on the server:

export class PageNotFoundComponent {
  private response = inject(RESPONSE_INIT);

  constructor() {
    // Only executes server-side
    if (this.response) {
      this.response.status = 404;
    }
  }
}

Check the docs for more reference

发布评论

评论列表(0)

  1. 暂无评论