Open mojtabanpe opened 1 year ago
You can add REQUEST and RESPONSE providers to the server.ts
commonEngine .render({ bootstrap: AppServerModule, documentFilePath: indexHtml, url:
${protocol}://${headers.host}${originalUrl}, publicPath: distFolder, providers: [ {provide: APP_BASE_HREF, useValue: baseUrl}, {provide: 'REQUEST', useValue: req}, {provide: 'RESPONSE', useValue: res}, ], }) .then((html) => res.send(html)) .catch((err) => next(err));
And then in your custom DeviceDetectorService you can inject REQUEST like this @Optional() @Inject('REQUEST') request: Request
Full example:
@Injectable() export class SsrDeviceDetectorService extends DeviceDetectorService { constructor(@Inject(PLATFORM_ID) platformId: any, @Optional() @Inject('REQUEST') request: Request) { super(platformId); if (isPlatformServer(platformId)) { super.setDeviceInfo((request.headers['user-agent'] as string) || ''); } } }
Probably only README.MD needs to be updated.
In Angular 17 the tokens aren't exported anymore by @angular/ssr.
Steps to reproduce the behavior:
Expected behavior The project should be compatible with this pull request i think.