简体   繁体   English

客户端渲染和跳过服务器渲染的方法?

[英]Client side rendering and skipping server rendering approach?

I want to render my pages client-side to help keep the load off the server and keep down traffic. 我想在客户端渲染页面,以帮助减轻服务器负担并降低流量。 I understand that the first call to the server normally results in that the server must render the page requested and then sends that page to the user. 我了解对服务器的第一次调用通常导致服务器必须呈现请求的页面,然后将该页面发送给用户。 When the user interacts with the page the javascript takes over the routing, which sends requests to the server for a specific view which then is updated via javascript on the client. 当用户与页面交互时,javascript将接管路由,路由将请求发送到服务器以获取特定视图,然后通过客户端上的javascript更新该视图。

I was thinking if this approach exists or not and if it is viable. 我在想这种方法是否存在以及是否可行。

Let's say i wish to go to the address: 假设我想去地址:

www.example.com/about

Is there an approach which attaches the /about as an argument to the html page/javascript code (same that serves www.example.com/ ) from the server. 是否有一种方法可以将/about作为参数附加到服务器的html页面/ javascript代码(与www.example.com/相同)。 When the dom is ready on the client it sends a request to the server for the /about data, which with the response updates the clients views. 当dom在客户端上准备就绪时,它将向服务器发送/about数据请求,并通过响应更新客户端视图。 This would mean skipping the server side rendering completely. 这意味着完全跳过服务器端渲染。

Or am I crazy? 还是我疯了?

No, you're not crazy. 不,你不疯。 This is the way that most single page application (SPA) frameworks, like AngularJS work. 这就是大多数单页应用程序(SPA)框架(例如AngularJS)的工作方式。

For example, the way that I typically configure a webserver for AngularJS is to have the following routes: 例如,我通常为AngularJS配置Web服务器的方式是具有以下路由:

  • /pub/<relative_file_path> just serves the static file specified by <relative_file_path> , such as all the JS, HTML partials and CSS. /pub/<relative_file_path>只是用于通过指定的静态文件<relative_file_path>如所有的JS,HTML泛音和CSS。
  • /api/<whatever> is where all endpoints which the client accesses via AJAX live /api/<whatever>是客户端通过AJAX访问的所有终结点所在的位置
  • everything else just returns index.html , which is a static file that contains the whole page with references to the JS, CSS and markup. 其他所有内容都只返回index.html ,这是一个静态文件,包含整个页面,其中包含对JS,CSS和标记的引用。

Thus when you go to www.example.com/about , you will get the index.html page, which tells the browser to download the JS for the whole application, which includes the client-side routing configuration. 因此,当您访问www.example.com/about ,将获得index.html页面,该页面告诉浏览器下载整个应用程序的JS,其中包括客户端路由配置。 Once the application starts, it sees that the location is www.example.com/about , and says "I know about /about , I will go to that page!". 应用程序启动后,它会看到该位置为www.example.com/about ,并说“我知道/about ,我将转到该页面!”。

Of course, by "go to that page", I mean "fetch the partial HTML for that page, and fetch any data via AJAX". 当然,“转到该页面”是指“获取该页面的部分HTML,并通过AJAX获取任何数据”。 No visible page refresh/change happens. 没有可见的页面刷新/更改发生。

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

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