I want to use SSR to improve search engine optimization on my angular single page application. I have enabled SSR by using Angular-Universal which is working to serve the static html but my components heavily rely on asynchronous api calls to build the content on the page. See the example below where the $obs
is initialized in the ngOnInit
function and then used with the async
pipe in the html to pull out data. The problem I am having is the static html served by the SSR does not include anything that uses an async
pipe, these calls are made on the client side and then the content is loaded. This is not what I want because I need the content from the api call to generate meta tags. Is it possible to force angular universal to wait for the api call to finish, render the html with this data and then serve the static html?
@Component({
selector: 'app-example-component',
template: `
<div *ngIf="(obs$ | async) as model">
{{model.title}}
</div>
`
})
export class ExampleComponent implements OnInit {
obs$: Observable<SomeModel>;
constructor(private exampleHttpService: ExampleHttpService) {}
ngOnInit() {
this.obs$ = this.exampleHttpService.doSomeApiCall();
}
}
I want the SSR to render the template html with the title from the api call then serve that static html.