Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatibility with @angular/ssr #284

Open
mojtabanpe opened this issue Nov 12, 2023 · 1 comment
Open

Incompatibility with @angular/ssr #284

mojtabanpe opened this issue Nov 12, 2023 · 1 comment

Comments

@mojtabanpe
Copy link

In Angular 17 the tokens aren't exported anymore by @angular/ssr.

Steps to reproduce the behavior:

  1. Create a new Angular Project with --ssr option.
  2. Create a UnicersalDeviceDetector like in previous versions.
  3. You can not import REQUEST from anywhere.

Expected behavior
The project should be compatible with this pull request i think.

@mbrdar
Copy link

mbrdar commented Mar 16, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants