简体   繁体   English

AngularJS - 服务器端渲染

[英]AngularJS - server-side rendering

As you may know, AirBnb opensourced Rendr ( http://nerds.airbnb.com/weve-open-sourced-rendr ) which should enable server-side rendering of Backbone apps.如您所知,AirBnb 开源了 Rendr ( http://nerds.airbnb.com/weve-open-sourced-rendr ),它应该启用Backbone应用程序的服务器端渲染 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.这很酷,因为您可以在服务器上运行模板渲染的第一次“迭代”,客户端将获得完全渲染的 HTML 文档以及整个 JS 应用程序。 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 ?那么,有人设法用 jsdom 或 ZombieJS 渲染 AngularJS吗? 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 上的这些 dom/浏览器模拟理论上应该足以用于简单的服务器端 Angular 模板,但我发现在谷歌上搜索它的结果并不是很确定。

Here is another solution: https://github.com/ithkuil/angular-on-server这是另一个解决方案: 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).该 repo 的作者更新:那是大约 6 年前(在进行此编辑时)。 At this point, people should probably be using https://angular.io/guide/universal or just https://prerender.io/在这一点上,人们可能应该使用https://angular.io/guide/universal或只是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.这个新包https://github.com/a-lucas/angular.js-server允许您预渲染 Angular 应用程序并将 HTML 发送到客户端,然后客户端将执行 jS 代码。

It supports caching per url and you can define rules to activate URL pre-rendering.它支持每个 url 缓存,您可以定义规则来激活 URL 预呈现。

PS: I am the main contributor for this package. PS:我是这个包的主要贡献者。

AngularJS works withing jsdom context without any tricks. AngularJS 使用 jsdom 上下文没有任何技巧。 Just add angular.js to js src list and main page of angular app to jsdom on its initializion.只需在初始化时将 angular.js 添加到 js src 列表和 angular 应用程序的主页到 jsdom。

So, rendering is very simple: just use angular in jsdom and it works.因此,渲染非常简单:只需在 jsdom 中使用 angular 即可。 Putting it to browser is somewhat harder.把它放到浏览器上有点困难。

One way is batch syncing DOM changes.一种方法是批量同步 DOM 更改。

To get dynamical server-to-client updates you may use MutationEvents (unfortunatly, jsdom does't support MutationObservers, but MutationEvents work pretty fast).要获得动态的服务器到客户端更新,您可以使用 MutationEvents(不幸的是,jsdom 不支持 MutationObservers,但 MutationEvents 工作得非常快)。 Use them to stack up DOM changes in accumulator array and push it periodically to client browser (say, per 25 ms).使用它们在累加器数组中堆叠 DOM 更改,并定期将其推送到客户端浏览器(例如,每 25 毫秒)。

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 )这种方法的一种实现是 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.服务器端渲染的一个缺点是没有 DOM 框模型大小,因为要获得元素的宽度/高度,它应该被实际渲染。 Means this solution barely fits for svg and so on..意味着这个解决方案几乎不适合 svg 等等..

Also you may consider watching scope model and syncing it with browser-side scopes, but thats totally different story.您也可以考虑观看范围模型并将其与浏览器端范围同步,但那是完全不同的故事。

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.但是使用浏览器在服务器上呈现 html 并将其发送到前端不是一种选择。 Airbnb try it first but rejected because is slow and resource hungry. Airbnb 首先尝试但被拒绝,因为速度慢且资源匮乏。 It's not a production solution.这不是生产解决方案。

Update: This soon can be possible with the introduction of Object.observe ;)更新:随着 Object.observe 的引入,这很快就可以实现;)

AngularJS 2.0 can work on a server, too. AngularJS 2.0 也可以在服务器上工作。 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). Vojta Jina 在“JavaScript Jabber”节目#109 上谈到它 - http://javascriptjabber.com/109-jsj-dependency-injection-in-javascript-with-vojta-jina-misko-hevery/ (播放器中的 32:30 )。 There is a link to new AngularJS' dependency injection module - https://github.com/angular/di.js .有一个链接到新的 AngularJS 的依赖注入模块 - https://github.com/angular/di.js

@dai-shi created connect-prerenderer, see here . @dai-shi 创建了连接预渲染器,请参见此处 Still a few issues but hopefully a good start还有一些问题,但希望是一个好的开始

One approach is to route the HTML requests to nodejs server running phantomjs.一种方法是将 HTML 请求路由到运行 phantomjs 的 nodejs 服务器。 I used an approach based on phantomjs.我使用了一种基于 phantomjs 的方法。 Check it out if it solves看看能不能解决

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

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.我知道这有点晚了,angular.js-server ( https://github.com/a-lucas/angular.js-server ) 使用了 angular 的修改版本,它触发了检测何时所有必要的空闲状态ajax 请求和 $compile 事件被处理。

I managed to pre-render the mean.js stack with almost no modification.我设法在几乎没有修改的情况下预渲染了 mean.js 堆栈。

This is not performant, but I've been working on a simple PhantomJS server for Heorku that will parse any client JS.这不是高性能的,但我一直在为 Heorku开发一个简单的PhantomJS 服务器,它将解析任何客户端 JS。 I use it specifically with Angular and Rails to serve HTML to bot requests.我专门与 Angular 和 Rails 一起使用它来为机器人请求提供 HTML。

i Hope it could still help somone, but here is an npm package I have created:我希望它仍然可以帮助某人,但这是我创建的一个 npm 包:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM