AngularJS - server-side rendering AngularJS - server-side rendering node.js node.js

AngularJS - server-side rendering


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/


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.


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.