如何讓SSR,在PageNotFound頁面,StatusCode回應404

網站有做Server Side Rendering
在網址任意輸入,導到PageNotFound頁面時,該網頁的回應碼status code要是404

我試了stackoverflow幾個建議的方式
也試了 GitHub - patrickmichalina/fusebox-angular-universal-starter: Angular Universal seed project featuring Server-Side Rendering, @fuse-box bundling, material, firebase, Jest, Nightmare, and more
但用docker在本機都不成功
請問有人做過可以分享嗎?

是使用 angular universal 來做 SSR?

@Kevin

配合這篇作法回應404

在ssr時,瀏覽器的developer tools → network 可以發現
<router-outlet></router-outlet>沒有產生component相關的tag,e.g. <app-page></app-page>
我同事發現是window的關係
修改 app.server.module.ts後,就可以了

import { NgModule } from '@angular/core';
import { ServerModule } from '@angular/platform-server';
import { createWindow } from 'domino';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';

const window = createWindow() as any;
(global as any).window = window;
(global as any).document = window.document;
(global as any).Event = window.Event;
(global as any).KeyboardEvent = window.KeyboardEvent;

@NgModule({
  imports: [AppModule, ServerModule],
  bootstrap: [AppComponent],
})
export class AppServerModule {}
2個讚