Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server render part of DOM and inject it into Angular app

Let's consider we have a standard browser Angular (v.11 now) dashboard app: Dashboard contains widgets which are components that get data from API and display values (KPIs, charts, tables, etc).

The question is: Is there a way to render the widgets on server and injects them into dashboard DOM?

Since those widgets are Angular components my idea is whether Angular can run on server, compile the components into static DOM and inject it into browser running Angular app.

I tried Universal but that's really for pre-rendering first page, don't think it can be good for this scenario, or?

Thanks!

like image 662
George Knap Avatar asked Nov 18 '20 10:11

George Knap


People also ask

Is server-side rendering possible in angular?

Angular applications are client-side applications that execute on the browser - which means they are rendered on the client, not on the server. You can add server-side rendering to your app using Angular Universal.

What is Prerender in angular?

Angular Universal lets you prerender the pages of your application. Prerendering is the process where a dynamic page is processed at build time generating static HTML.

How is angular app rendered?

Angular utilizes Renderer abstraction to perform application rendering in an environment-agnostic way. The renderer is just an abstract class, so we're going to create an implementation which will render applications inside the system terminal using ASCII graphics.

Does angular run on client or server?

Angular Universal executes on the server, generating static application pages that later get bootstrapped on the client. This means that the application generally renders more quickly, giving users a chance to view the application layout before it becomes fully interactive.

What is server side rendering in Angular 8?

Angular 8 - Server Side Rendering. Server side Rendering (SSR) is a modern technique to convert a Single Page Application (SPA) running in the browser into a server based application. Usually, in SPA, the server returns a simple index.html file with the reference to the JavaScript based SPA app.

How to inject HTML page in angular?

Basically, In angular index.html page is served from express server for all the URL paths and that index.html page is passed through some express view engine which injects HTML into <app-root></app-root>, based on current route and component for that route.

How to manipulate Dom in angular?

There are many ways to manipulate the DOM in Angular. Let's use them instead of straight forward JavaScript approaches. Usually, there are two concepts in DOM Manipulations. Modifying DOM Elements. Modifying DOM Structure.

What host should I serve my angular application on?

Your application should be served on localhost:4201. When we use Angular Universal, the API that delivers the content is hit twice. First when the server is rendering the page and second, when the application is bootstrapped. This causes latency issues and a bad user experience as the screen usually flickers when this happens.


2 Answers

Universal can prerender all components of appmodule, as soon as you will go to route of that page universal will pre-render them as well, not the first page only.

If you don't find universal as perfect solution, you can use puppeteer for pre-rendering that page. please find the reference below.

https://developers.google.com/web/tools/puppeteer/articles/ssr

like image 139
Aakash Garg Avatar answered Sep 19 '22 22:09

Aakash Garg


If you don't want to use Angular Universal there are a few other ways to do this.

1) Inject the html into the index.html page when it is served.

I have done this with Asp.Net before. You serve the index.html page through a controller and use the controller to modify the page injecting the widget html into it. You use a template tag with an id so that the Angular app can query it and then insert into the DOM. Angular has a service called Renderer2 for doing DOM manipulations.

2) Use an http request to get the widget html

I have also done this before. You can make a http request once the app is loaded that returns the html as a string. You can then use the Renderer2 service to inject the html into the dom.

UPDATE

In my above answer I didn't address the rendering of the widgets part. One thing that hasn't been pointed out yet is that Angular compiles everything to JavaScript and not to static html so even if you found a way to compile them on the server - which there are ways, you wouldn't get static html.

There are couple of possibilities depending on whether you want the widgets injected when the index page is requested or when the user goes to the dashboard page . Just so you know I am just theorizing and have not tried any of this. You could dynamically import the JavaScript code. You could also create an html fragment with a script tag and load the html using one of the methods above. You would then have to dynamically create the selectors and add then to the DOM.

These would all take some research. Now I am curious if this can be done so I am going to try some of this out and see what happens.

UPDATE 2

Okay I am officially excited about trying this. I would use Docker to do this. I used Azure Pipelines and a Docker container a few years to compile an Angular library and create a npm package and upload it to a private npm registry. I think you could do the same thing for this without the npm part. I am not an expert in Docker but if you can dynamically install node then you should be able to make an api request to get the widget data and then write it into the widget.

like image 25
Andrew Alderson Avatar answered Sep 19 '22 22:09

Andrew Alderson