简体   繁体   English

客户端与服务器端模板(哪个?)

[英]Client-side vs. server-side templating (which one?)

I've been reading some very interesting articles about the whole client vs. server rendering lately. 最近,我一直在阅读一些有关整个客户端与服务器渲染的有趣文章。

http://www.onebigfluke.com/2015/01/experimentally-verified-why-client-side.html http://www.onebigfluke.com/2015/01/experimentally-verified-why-client-side.html

http://www.quirksmode.org/blog/archives/2015/01/angular_and_tem.html http://www.quirksmode.org/blog/archives/2015/01/angular_and_tem.html

http://tomdale.net/2015/02/youre-missing-the-point-of-server-side-rendered-javascript-apps/ http://tomdale.net/2015/02/youre-missing-the-point-of-server-side-rendered-javascript-apps/

Now I've been a bit of a fan boy when it comes to client side but after I read these articles some points started to show up in favor of the server side rendering, to my surprise... The main points were: 现在,当涉及到客户端时,我一直是一个狂热的小男孩,但是当我阅读这些文章后,出现了一些要点,它们倾向于服务器端渲染,这令我惊讶……要点是:

  • 1) You can upgrade your server, but not your users device - This means, well, yes... you are in control of the server, so if it's under performing you may opt to upgrade/scale. 1) 您可以升级服务器,但不能升级用户设备 -好的,是的, 可以控制服务器,因此,如果服务器性能不佳,则可以选择升级/扩展。 You can't force users to upgrade their devices. 您不能强迫用户升级其设备。

  • 2) First paint vs. last paint - Now on the experimentally verified... link above it shows when the users first see the page (first paint) and when the users may use the page 100% (last paint). 2) 第一种涂料与最后一种涂料 -现在经过experimentally verified...链接上显示了用户何时首次看到页面(第一种涂料)以及用户何时可以使用该页面的100%(最后一种涂料)。 Now from what I can think of when the user sees the page, it takes their brain some time to process the signals from the visual cortex to the frontal cortex and then to the premoter cortex where the user actually starts clicking his/her finger, that is of course if the html is rendered first so the brain has something to process while loading is happening in the background (js files, binding etc.). 现在,从我能想到的内容来看,用户看到页面时,需要花一些时间来处理从视觉皮层到额叶皮层,再到运动前皮层的信号,然后用户才开始点击他/她的手指,当然,如果先渲染html,则在后台进行加载时,大脑有一些需要处理的内容(js文件,绑定等)。

What really got me was the bit were twitter reported people of having up to 10 seconds of loading time for client side rendering, no one should ever experience that ! 真正让我感兴趣的是,据Twitter报道,人们有多达10秒的加载时间用于客户端渲染, 没有人会经历过 It's kind of saying, " Well if you don't have a good enough device, sorry, you'll just have to wait. ". 有点说,“ 好吧,如果您没有足够好的设备,对不起,您只需等待。

I've been thinking, isn't there a good way of using both client-side and server-side templating engines and which both client and server use the same template engine and code . 我一直在思考,没有同时使用客户端和服务器端模板引擎的好方法,以及客户端和服务器使用相同模板引擎和代码的好方法 In that case it's only to figure out if it's benefactor to supply the client with the rendered page or let the client render it themselves. 在这种情况下,只能弄清楚是为客户端提供呈现的页面还是让客户端自己呈现页面是有益的。

In any case, share your thoughts on my sayings and the articles if you want. 无论如何,如果需要,请分享您对我的观点和文章的看法。 I'm all ears! 我全是耳朵!

UPD: do it only if you really need it UPD:仅在确实需要时执行此操作

(4 years and 2 isomorphic enterprise apps later) (4年和2个同构企业应用程序以后)

If you're required to do SSR, fine. 如果需要执行SSR,则可以。 If you can go with a simple SPA - go with it. 如果可以使用简单的SPA,请选择它。

Why so? 为什么这样? SPAs are easier to develop, easier to debug and cheaper and easier to run. SPA 更易于开发, 更易于调试, 更便宜且更易于运行。

The development and debugging complications are evident. 开发和调试的复杂性显而易见。 What do I mean by "cheaper and easier to run", though? 但是,“更便宜,更容易运行”是什么意思? Well, guess what, if 10K users try to open your app at the same time your static HTML website (ie a built SPA) you won't even feel it. 好吧,猜猜是什么呢,如果1万用户尝试在您的静态HTML网站(即内置SPA)的同时打开您的应用程序,您什至不会感觉到。 If you're running an isomorphic webapp though, the TTFB will go up, RAM usage will go up and eventually you'll have to run a cluster of those. 如果您正在运行同构Web应用程序,则TTFB将增加,RAM使用率将增加,最终您将必须运行其中的一个集群。

So, unless you are required to show some super-low TTFB times (which will likely come through aggressive caching), don't overcomplicate your life. 因此,除非要求您显示一些超低的TTFB时间(这很可能是通过主动缓存),否则不要使您的生活变得过于复杂。

Original answer from 2015: 2015年的原始答案:

Basically you're looking for an isomorphic web app that shares the same code for frontend and backend. 基本上,您正在寻找一个同构的Web应用程序 ,该应用程序共享相同的前端和后端代码。

Isomorphic JavaScript 同构JavaScript

JavaScript applications which run both client-side and server-side. 同时在客户端和服务器端运行的JavaScript应用程序。 Isomorphic JavaScript frameworks are the next step in the evolution of JavaScript frameworks. 同构JavaScript框架是JavaScript框架发展的下一步。 These new libraries and frameworks are solving the problems associated with traditional JavaScript frameworks. 这些新的库和框架正在解决与传统JavaScript框架相关的问题。

I bet this guy explains that much better that me. 我敢打赌, 这家伙向解释得更好

在此处输入图片说明

So, when a user comes to the page, the server renders the full page with contents. 因此,当用户访问该页面时,服务器将使用内容呈现整个页面。 So it loads faster and requires no extra ajax requests to load data, etc. Then, when a user navigates to another page, the usual techniques for single page applications are used. 因此,它加载速度更快,并且不需要额外的ajax请求来加载数据等。然后,当用户导航到另一页时,将使用单页应用程序的常用技术。

So, WHY WOULD I CARE? 所以,我为什么要关心?

  • Old browsers / Weak devices / Disabled Javascript 旧版浏览器/弱设备/禁用Javascript
  • SEO 搜索引擎优化
  • Some page load improvements 某些页面加载方面的改进

Old browsers / Weak devices / Disabled Javascript 旧版浏览器/弱设备/禁用Javascript

For example, IE9 does not support History API. 例如,IE9不支持History API。 So, for old browsers (and if user disables javascript too), they would just navigate through pages just like they did it it in good old days. 因此,对于旧版浏览器(如果用户也禁用了javascript),它们将像浏览旧版一样浏览页面。

SEO 搜索引擎优化

Google says it supports SPA's but SPA's aren't likely to appear in the top results of google search, are they? Google表示它支持SPA,但是SPA不太可能出现在Google搜索的顶部结果中,对吗?

Page speed 页面速度

As it was stated, the first page loads with one HTTP request, and that's all. 如前所述,第一页加载了一个HTTP请求,仅此而已。

OK, so 好啦

There are lots of articles on that: 有很多关于此的文章:

But SHOULD I CARE? 但是我应该照顾吗?

It's up to you, of course. 当然,这取决于您。

Yeah, that's cool, but it takes much work to rewrite/adapt the existing app. 是的,这很酷,但是重写/适应现有应用程序需要花费大量工作。 And if your backend is in PHP/Ruby/Python/Java/Whatever, I've got bad news for you (it's not necessarily impossible, but close to that). 而且,如果您的后端位于PHP / Ruby / Python / Java / Whatever中,那么我对您来说是个坏消息(不一定是不可能的,但很可能是这样)。

It depends on the website, you can try to collect some stats and if the percentage of users with old devices is small, it's not worth the trouble, so why not... 这取决于网站,您可以尝试收集一些统计信息,如果使用旧设备的用户所占的比例很小,那是不值得的,所以为什么不...

LET THEM SUFFER 让他们感到沮丧

If you care only about users with old devices, then c'mon, it 2015, and it's your user's problem if he's using IE8 of browsing websites with a iPod Touch 2. For example, Angular dropped IE8 support in 1.3 approximately a year ago, so why wouldn't you just alert the users that they need to upgrade ;) 如果您只关心使用旧设备的用户,那么请看2015年,如果用户使用IE8浏览带有iPod Touch 2的网站是用户的问题。例如,大约一年前,Angular在1.3中放弃了IE8支持,那么,为什么不只提醒用户他们需要升级;)

Cheers! 干杯!

All of the conversations on this topic miss one point. 关于该主题的所有对话都遗漏了一点。 Bytes sent to the client. 发送给客户端的字节。 Pages rendered as HTML on the server are a lot smaller. 在服务器上以HTML呈现的页面要小得多。 Less bytes transmitted is better for everyone, both server and client. 传输的字节数越少对服务器和客户端的每个人都越好。 I've seen the bandwidth costs on cloud sites and even a 10% reduction can be a huge saving. 我已经看到了云站点上的带宽成本,即使减少10%也可以节省很多。 Client side JS pages are always fat. 客户端JS页面总是很胖。

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

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