|
| 1 | +# Deepkit + Angular SSR |
| 2 | + |
| 3 | +## Configure Deepkit App |
| 4 | + |
| 5 | +- Make sure to put your main application inside `app.ts` and export the `App` instance: |
| 6 | + |
| 7 | +In `src/server/app.ts`: |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { App } from '@deepkit/app'; |
| 11 | +import { FrameworkModule } from '@deepkit/framework'; |
| 12 | +import { AngularModule } from '@deepkit/angular-ssr'; |
| 13 | + |
| 14 | +export const app = new App({ |
| 15 | + controllers: [ |
| 16 | + // your controllers |
| 17 | + ], |
| 18 | + providers: [ |
| 19 | + // your providers |
| 20 | + ], |
| 21 | + imports: [ |
| 22 | + new FrameworkModule({}), |
| 23 | + new AngularModule() |
| 24 | + ] |
| 25 | +}); |
| 26 | +``` |
| 27 | + |
| 28 | +Create a `src/server/server.ts` and configure it in your `angular.json`: |
| 29 | + |
| 30 | +```json |
| 31 | +{ |
| 32 | + "builder": "@angular-devkit/build-angular:application", |
| 33 | + "options": { |
| 34 | + "outputPath": "dist/app", |
| 35 | + "index": "src/index.html", |
| 36 | + "server": "src/main.server.ts", |
| 37 | + "outputMode": "server", |
| 38 | + "ssr": { |
| 39 | + "entry": "src/server/server.ts" |
| 40 | + }, |
| 41 | + "browser": "src/main.ts" |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +In `src/server/server.ts`: |
| 47 | + |
| 48 | +```typescript |
| 49 | +import { app } from './app'; |
| 50 | +import { RequestHandler } from '@deepkit/angular-ssr'; |
| 51 | + |
| 52 | +export const reqHandler = app.get(RequestHandler).create(import.meta.url); |
| 53 | +```` |
| 54 | + |
| 55 | +## Configure Angular App |
| 56 | + |
| 57 | +In `app/app.config.ts` (client side): |
| 58 | + |
| 59 | +```typescript |
| 60 | +@Injectable() |
| 61 | +export class APIInterceptor implements HttpInterceptor { |
| 62 | + constructor(@Inject('baseUrl') @Optional() private baseUrl: string) { |
| 63 | + // In client build, `baseUrl` is empty and should be inferred from the current location. |
| 64 | + // If this is not correct, you can simply define the `baseUrl` in the `providers` array of the `appConfig` object. |
| 65 | + this.baseUrl = baseUrl || (typeof location !== 'undefined' ? location.origin : ''); |
| 66 | + } |
| 67 | +
|
| 68 | + intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
| 69 | + const apiReq = req.clone({ url: `your-api-url/${req.url}` }); |
| 70 | + return next.handle(apiReq); |
| 71 | + } |
| 72 | +} |
| 73 | +
|
| 74 | +export const appConfig: ApplicationConfig = { |
| 75 | + providers: [ |
| 76 | + { |
| 77 | + provide: HTTP_INTERCEPTORS, |
| 78 | + useClass: APIInterceptor, |
| 79 | + multi: true, |
| 80 | + }, |
| 81 | + // your other providers |
| 82 | + ], |
| 83 | +}; |
| 84 | +``` |
| 85 | + |
| 86 | +In `app/app.server.config.ts` (server side): |
| 87 | + |
| 88 | +```typescript |
| 89 | +import { appConfig } from './app.config'; |
| 90 | +
|
| 91 | +const serverConfig: ApplicationConfig = { |
| 92 | + providers: [ |
| 93 | + provideServerRendering(), |
| 94 | + provideServerRouting([ |
| 95 | + { |
| 96 | + path: '**', |
| 97 | + renderMode: RenderMode.Server, |
| 98 | + }, |
| 99 | + ]), |
| 100 | + { |
| 101 | + provide: HTTP_TRANSFER_CACHE_ORIGIN_MAP, |
| 102 | + deps: [REQUEST_CONTEXT], |
| 103 | + useFactory(context: any) { |
| 104 | + return { [context?.baseUrl]: context?.publicBaseUrl || '' }; |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + provide: 'baseUrl', |
| 109 | + deps: [REQUEST_CONTEXT], |
| 110 | + useFactory: (context: any) => { |
| 111 | + return context?.baseUrl || ''; |
| 112 | + }, |
| 113 | + } |
| 114 | + ], |
| 115 | +}; |
| 116 | +
|
| 117 | +export const config = mergeApplicationConfig(appConfig, serverConfig); |
| 118 | +``` |
0 commit comments