Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

SSR APP_INITIALIZER HttpClient error #657

Open

Description

@xiongyayun428
@NgModule({
  declarations: [AppComponent, HomeComponent],
  imports: [
    BrowserModule.withServerTransition({ appId: 'my-app' }),
    RouterModule.forRoot([
      { path: '', component: HomeComponent, pathMatch: 'full' },
      { path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule' },
      { path: 'lazy/nested', loadChildren: './lazy/lazy.module#LazyModule' }
    ]),
    HttpClientModule,
    TransferHttpCacheModule,
    StartupModule.forRoot('/assets/config/app.json'),
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}
import { NgModule, ModuleWithProviders, APP_INITIALIZER } from '@angular/core';
import { StartupService, STARTUP_CONFIG } from './startup.service';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  imports: [
    HttpClientModule
  ],
})
export class StartupModule {
  static forRoot(startupConfig: string): ModuleWithProviders<StartupModule> {
    return {
      ngModule: StartupModule,
      providers: [
        { provide: STARTUP_CONFIG, useValue: startupConfig },
        StartupService,
        {
          provide: APP_INITIALIZER,
          useFactory: StartupServiceFactory,
          deps: [StartupService, STARTUP_CONFIG],
          multi: true,
        }
      ]
    };
  }
}

export function StartupServiceFactory(startService: StartupService, config: string): Function {
  return () => startService.load(config).then(resp => console.log(resp));
}
import { Injectable, InjectionToken } from '@angular/core';
import { HttpClient } from '@angular/common/http';

export const STARTUP_CONFIG = new InjectionToken<string>(
  'STARTUP_CONFIG'
);

@Injectable()
export class StartupService {
  constructor(private client: HttpClient) {}

  load(config: string): Promise<any> {
    return this.client
      .get(config)
      .toPromise()
      .catch(error => {
        console.error(`==========================================`, error);
      });
  }
}

result:
image

use StartupModule.forRoot('./assets/config/app.json') is error
use StartupModule.forRoot('http://localhost:4000/assets/config/app.json') is successed

https://stackblitz.com/edit/universal-starter

Activity

  1. mailiam commented on Apr 2, 2019

    @mailiam

    Hey, It is relieving to find someone who had same problem that made me confused.

    I found out that this issue is actually officially documented, with simple interceptor example:
    https://angular.io/guide/universal#using-absolute-urls-for-server-requests

    hope someone else stumbling upon same problem find this useful :]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions