AngularJS - server-side rendering

node.jsAngularjsTemplatesServer Side-Renderingzombie.js

node.js Problem Overview


As you may know, AirBnb opensourced Rendr (http://nerds.airbnb.com/weve-open-sourced-rendr) which should enable server-side rendering of Backbone apps. This is cool, because one can run the first "iteration" of template rendering on the server and the client gets fully rendered HTML document plus the whole JS app. It greatly reduces time-to-display and can get us rid of other server-side templating systems.

So, have someone managed to render AngularJS with jsdom or ZombieJS? These dom/browser emulations on Node.js should in theory be enough for simple server-side Angular templating, but results I found googling it were not very conclusive.

node.js Solutions


Solution 1 - node.js

Here is another solution: https://github.com/ithkuil/angular-on-server

wiki for details

Update from author of that repo: that was about 6 years ago (at the time of this edit). At this point, people should probably be using https://angular.io/guide/universal or just https://prerender.io/

Solution 2 - node.js

This new package https://github.com/a-lucas/angular.js-server allows you to pre-render an Angular application and send HTML to the client, that will then execute the jS code.

It supports caching per url and you can define rules to activate URL pre-rendering.

PS: I am the main contributor for this package.

Solution 3 - node.js

AngularJS works withing jsdom context without any tricks. Just add angular.js to js src list and main page of angular app to jsdom on its initializion.

So, rendering is very simple: just use angular in jsdom and it works. Putting it to browser is somewhat harder.

One way is batch syncing DOM changes.

To get dynamical server-to-client updates you may use MutationEvents (unfortunatly, jsdom does't support MutationObservers, but MutationEvents work pretty fast). Use them to stack up DOM changes in accumulator array and push it periodically to client browser (say, per 25 ms).

Also to enable user events, you should track them document-wise on browser and similarry accumulate and push them to server.

One implementation of such approach is jsdom-sync (https://www.npmjs.org/package/jsdom-sync)

A downside of server side rendering is absence of DOM box model size, because to get element width/height it should be actually rendered. Means this solution barely fits for svg and so on..

Also you may consider watching scope model and syncing it with browser-side scopes, but thats totally different story.

Solution 4 - node.js

I'm searching for a solution too. But it's not an option to use browser to render the html on the server and send it to the frontend. Airbnb try it first but rejected because is slow and resource hungry. It's not a production solution.

Update: This soon can be possible with the introduction of Object.observe ;)

Solution 5 - node.js

AngularJS 2.0 can work on a server, too. Vojta Jina talks about it on "JavaScript Jabber" show #109 - http://javascriptjabber.com/109-jsj-dependency-injection-in-javascript-with-vojta-jina-misko-hevery/ (32:30 in player). There is a link to new AngularJS' dependency injection module - https://github.com/angular/di.js.

Solution 6 - node.js

@dai-shi created connect-prerenderer, see here. Still a few issues but hopefully a good start

Solution 7 - node.js

One approach is to route the HTML requests to nodejs server running phantomjs. I used an approach based on phantomjs. Check it out if it solves

http://himangshu.io/blog/optimizing-single-page-application-using-prerender/

Solution 8 - node.js

I know this is a bit a late answer, angular.js-server (https://github.com/a-lucas/angular.js-server) uses a modified version of angular that triggers an idle state necessary to detect when all ajax request and $compile events are processed.

I managed to pre-render the mean.js stack with almost no modification.

Solution 9 - node.js

This is not performant, but I've been working on a simple PhantomJS server for Heorku that will parse any client JS. I use it specifically with Angular and Rails to serve HTML to bot requests.

Solution 10 - node.js

i Hope it could still help somone, but here is an npm package I have created:

https://www.npmjs.com/package/ng-node-compile

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontilldaView Question on Stackoverflow
Solution 1 - node.jsoutluchView Answer on Stackoverflow
Solution 2 - node.jsAntoine LucasView Answer on Stackoverflow
Solution 3 - node.jssetecView Answer on Stackoverflow
Solution 4 - node.jsAleksandrenkoView Answer on Stackoverflow
Solution 5 - node.jsbullgareView Answer on Stackoverflow
Solution 6 - node.jsLiorView Answer on Stackoverflow
Solution 7 - node.jshimangshujView Answer on Stackoverflow
Solution 8 - node.jsAntView Answer on Stackoverflow
Solution 9 - node.jssmothersView Answer on Stackoverflow
Solution 10 - node.jsMoLowView Answer on Stackoverflow