APP_INITIALIZER with SSR problem in Angular 17

Discussão em 'Angular' iniciado por Seto, Novembro 15, 2024.

  1. Seto

    Seto Guest

    guys. I encountered a problem when I try to migrate to SSR in Angular 17. My app is client only in Angular 16. I have a service named CiyuanEnvironmentService and another service named LiveService. The LiveService access the CiyuanEnvironmentService environment property which is loaded with HttpClient asynchronously, and I make the method as factory method or APP_INITIALIZER. In non-SSR, it will finished before the LiveService access it. But in SSR, it seems that it will access to it before it's ready. How can I solve it? Or is there another one for SSR? Thank you.

    CiyuanEnvironmentService,
    {
    provide: APP_INITIALIZER,
    useFactory: (ciyuanEnvironmentService: CiyuanEnvironmentService) => () => ciyuanEnvironmentService.loadEnvironment(),
    deps: [CiyuanEnvironmentService],
    multi: true
    }


    It seem that I should use this with SSR.

    loadEnvironment() {
    let ret = this.httpClient.get<CiyuanEnvironment>(environment.ciyuanEnvironmentFile)
    ret.subscribe(config => {
    this.environment = config;
    });
    return ret;
    }


    Previously, this is working in client only app.

    loadEnvironment() {
    return this.httpClient.get<CiyuanEnvironment>(environment.ciyuanEnvironmentFile).subscribe(config => {
    this.environment = config;
    });
    }


    Is this intended?

    Continue reading...

Compartilhe esta Página