简体   繁体   中英

Angular 5 Universal wait for http request to return during server-side render

I have an Angular 5 app that is served using ngExpressEngine (Followed the Angular Universal starter project). My app has a component that makes a HTTP request to to get some data to be displayed. All works correctly but when I use fetch as google bot it returns the rendered page but none of the data. Is there any make the ngExpressEngine server to wait for HTTP requests before rendering the page back to the user?

I don't know about Angular 5, but with the latest Angular release (7.2 at the time I write this), I can confirm that all the data loaded via a Resolve is properly rendered on server side, and also all the data loaded via ngOnInit hooks, even if it is asynchronous.

That works thanks to Zone.js, indeed, it tracks all of the asynchronous operations and emits an onMicrotaskEmpty event when the asynchronous task queue is empty to let Angular know that the page is ready for rendering. For reference: what is the use of Zone.js in Angular 2

You can use APP_INITIALIZER in your provider to delay app initialization until fetching your api from your client app module like:

//app.module.ts
        providers: [{provide: APP_INITIALIZER, deps: [HttpClient], multi: true,
    useFactory: (http: HttpClient: Promise) => new Promise<Your Data Interface>((resolve, reject) => http.get("").pipe(map(data => {
     //do something with data
resolve(data)
    }), catchError(error => of(error).pipe(map(() => //do something if error)))))}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM