75

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.

shA.t
  • 16,580
  • 5
  • 54
  • 111
tillda
  • 18,150
  • 16
  • 51
  • 70
  • 7
    It may be coming in AngularJS 2.0. In [this video](http://youtu.be/ZhfUv0spHCY?t=40m30s) they talk about server-side rendering & why it currently isn't possible to do such a thing with AngularJS at the moment. – Jay Apr 26 '13 at 19:44
  • did any of the answers work for you ? – Xsmael Mar 16 '18 at 14:06

10 Answers10

16

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/

Jason Livesay
  • 6,317
  • 3
  • 25
  • 31
outluch
  • 841
  • 7
  • 15
  • 4
    the angular.io thing is for Angular, not AngularJs. They are two completely different languages... – bokkie Jul 10 '19 at 12:30
  • Can I use Prerender in an **[application](https://codereview.stackexchange.com/questions/232741/blogging-application-with-codeigniter-back-end-and-angularjs-front-end)** that uses **PHP** on the server-side, instead of Express? How? – Razvan Zamfir Nov 26 '19 at 11:03
7

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.

Antoine Lucas
  • 101
  • 1
  • 3
5

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.

setec
  • 15,506
  • 3
  • 36
  • 51
4

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 ;)

Aleksandrenko
  • 2,987
  • 6
  • 28
  • 34
  • In most cases server-side rendering will used for robots and some special cases, so server performance impact is neglible. Also 'rendering' on server side is just template to html processing, without actual rendering (which is most CPU hungry), which anyway occurs at client side. – setec Nov 02 '14 at 08:45
  • usually you want to do the server side rendering on an initial load. so user x is going to a list page for the first time visiting the app. maybe they closed the browser and reopened it later. instead of loading the app, then making another http request for the data, the server can just do all of that for us in the initial launch. the other very useful reason to do this is for search engine bots. not all of them are as smart as the google bots and don't have their own javascript compilers. – jemiloii Mar 02 '16 at 23:06
  • Render time does matter for google Adwords bots which use load time as a factor in [landing page quality](https://support.google.com/adwords/answer/2404197?co=ADWORDS.IsAWNCustomer%3Dfalse&hl=en). I've found even headless browsers like PhantomJS generate significant CPU load. – Edward Newell Sep 13 '16 at 17:17
4

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.

bullgare
  • 1,643
  • 1
  • 21
  • 32
2

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

Lior
  • 40,466
  • 12
  • 38
  • 40
  • What If I have an **[application](https://codereview.stackexchange.com/questions/232741/blogging-application-with-codeigniter-back-end-and-angularjs-front-end)** that uses **PHP** on the server-side, instead of Express? What option do I have? – Razvan Zamfir Nov 26 '19 at 11:02
2

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/

himangshuj
  • 730
  • 5
  • 9
2

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.

Ant
  • 1,812
  • 5
  • 22
  • 39
1

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.

smothers
  • 434
  • 5
  • 15
-1

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

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

MoLow
  • 3,056
  • 2
  • 21
  • 41