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

server side rendering - How to set providers in Angular v19 SSR? - Stack Overflow

programmeradmin0浏览0评论

In Angular <19 I used such code to specify the providers:

  // All regular routes use the Angular engine
  server.get('**', (req, res, next) => {
    const { protocol, originalUrl, baseUrl, headers } = req;

    commonEngine
      .render({
        bootstrap,
        documentFilePath: indexHtml,
        url: `${protocol}://${headers.host}${originalUrl}`,
        publicPath: distFolder,
        providers: [
          { provide: APP_BASE_HREF, useValue: baseUrl },
          { provide: RESPONSE, useValue: res },
          { provide: REQUEST, useValue: req },
        ],
      })
      .then((html) => res.send(html))
      .catch((err) => next(err));
  });

but in Angular 19 there is another structure:

app.use('/**', (req, res, next) => {
  angularApp
    .handle(req)
    .then((response) =>
      response ? writeResponseToNodeResponse(response, res) : next(),
    )
    .catch(next);
});

I have tried to add the providers array in the second parameter of handle() method, but it did not work for me.

I need to set providers for the REQUEST token, which should be the req object

In Angular <19 I used such code to specify the providers:

  // All regular routes use the Angular engine
  server.get('**', (req, res, next) => {
    const { protocol, originalUrl, baseUrl, headers } = req;

    commonEngine
      .render({
        bootstrap,
        documentFilePath: indexHtml,
        url: `${protocol}://${headers.host}${originalUrl}`,
        publicPath: distFolder,
        providers: [
          { provide: APP_BASE_HREF, useValue: baseUrl },
          { provide: RESPONSE, useValue: res },
          { provide: REQUEST, useValue: req },
        ],
      })
      .then((html) => res.send(html))
      .catch((err) => next(err));
  });

but in Angular 19 there is another structure:

app.use('/**', (req, res, next) => {
  angularApp
    .handle(req)
    .then((response) =>
      response ? writeResponseToNodeResponse(response, res) : next(),
    )
    .catch(next);
});

I have tried to add the providers array in the second parameter of handle() method, but it did not work for me.

I need to set providers for the REQUEST token, which should be the req object

Share Improve this question asked Nov 20, 2024 at 15:13 SergejSergej 2,2161 gold badge21 silver badges33 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 6

In v19 those token are set for you automatically by the framework.

In your app, you'll pull them from '@angular/core'

import { Component, inject REQUEST, } from '@angular/core';

@Component({...})
export class AppComponent {
  constructor() {
    const request = inject(REQUEST, { optional: true });
  }
}

Make sure to inject is with optional: true as this token will not be available in every context.

I have a working demo you can check: https://github/jeanmeche/ssr-v19

Additional note : If you want to inject custom tokens from your node server and provide them to your angular v19 app, you can use REQUEST_CONTEXT from @angular/core.

server.ts

app.use('/**', (req, res, next) => {
  angularApp
    .handle(req, {
      context: { foo: 'bar' },
    })
    .then((response) =>
      response ? writeResponseToNodeResponse(response, res) : next(),
    )
    .catch(next);
}); 

Then use TransferState API to get your data in the browser.

const CONTEXT_KEY = makeStateKey<any>('context');

@Injectable({
  providedIn: 'root',
})
export class ServerContextService {
  constructor(
  ) {
    const transferState = inject(TransferState);
    const CONTEXT_TOKEN:any = inject(REQUEST_CONTEXT, { optional: true });

    if(CONTEXT_TOKEN) {
      transferState.set(CONTEXT_KEY, CONTEXT_TOKEN);
    }

    this.transfered = transferState.get(CONTEXT_KEY, {});
  }

  transfered: any;
}
发布评论

评论列表(0)

  1. 暂无评论