-->

Angular 4 SSR output doesn't render some of th

2019-08-29 07:55发布

问题:

I have a problem with a HTML generated by SSR (server side rendering with Node.js) using 'Angular Universal'. In most of the cases in my application - SSR is working pretty well, but two cases didnt work at all.

1. <title> tag content

I have some default value inside title tag, which I'm changing in particular routes after retrieving some data from HTTP request. To do this, I'm using:

import { Title } from '@angular/platform-browser';
titleService.setTitle('NewTitle');  

From the browser/user perspective - it's working completelly fine. But when I get into source of the page in the browser or I will retrieve source of the page using curl - new value from HTTP call is not visible, only default one.

2. Value inside <script type="application/ld+json">

One of the requirements in my project is dynamically generating datasets (https://developers.google.com/search/docs/data-types/dataset) for subpages of the application. I tried to use solution from this tutorial: https://coryrylan.com/blog/angular-seo-with-schema-and-json-ld

but JSON generated asynchronously using HTTP call, is undefined inside <ngx-json-ld> tags.

I also tried to do this by myself without third party libraries, using innerHtml:

<div [innerHtml]="jsonLDString"></div>

Where jsonLDString is sanitized by:

 this.sanitizer.bypassSecurityTrustHtml(this.jsonLDString);

The output is exactly the same as with the first problem: only default value is rendered with SSR, proper values could be seen if you are looking on the page by 'inspect element' option in the browser.