使用 javascript 完全生成 html 的优缺点

发布于 2024-12-22 09:35:31 字数 957 浏览 1 评论 0 原文

最近,我想出了一些关于如何提高整体性能的想法 一个 Web 应用程序,为什么不让它在客户端完全生成,而不是从 Web 服务器生成准备显示的 html 页面。 这样做,只需将纯数据(在我的例子中是 JSON 格式的数据)发送到浏览器。 这会将 html 生成工作从服务器转移到客户端浏览器, 减少发送回用户的响应数据包的大小。

经过一番研究,我发现这个框架(http://beebole-apps.com/pure/) 和我做同样的事情。

我想知道的是这样做的利弊。 对于 Web 服务器来说,它肯定更快更好,并且使用现代浏览器,Javascript 代码可以快速运行,因此可以快速完成页面生成。

这种方法的缺点可能是 SEO。 我不确定像谷歌这样的搜索引擎是否会适当地索引我的网站。 你能告诉我这个方法的缺点是什么吗?

Ps: 我还附上了示例代码来帮助描述该方法,如下所示:

在 head 中,简单的 javascript 代码将这样编写:

<script type='javascript' src='html_generator.js'/>
<script>
   function onPageLoad(){
      htmlGenerate($('#data').val());
   } 
</script>

在 body 中,仅存在一个元素,仅用作数据容器,如下:

  <input type='hidden' id='data' value='{"a": 1, "b": 2, "c": 3}'/> 

当浏览器渲染文件时,将调用 html_generator.js 中的 htmlGenerate 函数,并在该函数中生成整个 html 页面。请注意,html_generator.js 文件可能很大,因为它包含大量 html 模板,但由于它可以缓存在浏览器中,因此只会下载一次。

Recently, I have come up with some idea about how to improve the overall performance for
a web application in that instead of generating a ready-to-show html page from the web server, why not let it be fully generated in the client side.
Doing it this way, only pure data (in my case is data in JSON format) needs to be sent to the browser.
This will offload the work of html generation from the server to the client's browser and
reduce the size of the response packet sent back to users.

After some research, I have found that this framework (http://beebole-apps.com/pure/)
does the same thing as mine.

What I want to know is the pros and cons of doing it this way.
It is surely faster and better for web servers and with modern browser, Javascript code can run fast so page generation can be done fast.

What should be a downside for this method is probably for SEO.
I am not sure if search engines like Google will appropriately index my website.
Could you tell me what the downside for this method is?

Ps: I also attached sample code to help describe the method as follows:

In the head, simple javascript code will be written like this:

<script type='javascript' src='html_generator.js'/>
<script>
   function onPageLoad(){
      htmlGenerate($('#data').val());
   } 
</script>

In the body, only one element exist, used merely as a data container as follows:

  <input type='hidden' id='data' value='{"a": 1, "b": 2, "c": 3}'/> 

When the browser renders the file, htmlGenerate function which is in html_generator.js will be called and the whole html page will be generated in this function. Note that the html_generator.js file can be large since it contains a lot of html templates but since it can be cached in the browser, it will be downloaded once and only once.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

活雷疯 2024-12-29 09:35:31

缺点

  • 搜索引擎将无法索引您的页面。如果他们这样做了,你就非常幸运了。
  • 禁用 JavaScript 的用户或使用移动设备的用户很可能无法使用它。
  • 速度优势可能微乎其微,尤其是当用户使用 IE 等速度较慢的 JavaScript 引擎时。
  • 可维护性:除非你自动生成 JavaScript,否则这将是一场噩梦!

总而言之,

如果您只是为了提高速度,则不建议使用此方法。然而,这通常是在 Web 应用程序中完成的,用户停留在同一页面上,但您可能最好使用现有框架之一来实现此目的,例如 backbone.js,并通过遵循 Google 的 hashbang 指南HTML5 PushState (由 @rohk 建议)。

如果您只是寻求性能,并且您的应用程序并不严格需要这样工作,请不要这样做。但如果您确实这样做,那么请确保以标准化方式进行,以便保持快速和可索引。

Downsides

  • Search Engines will not be able to index your page. If they do, you're very lucky.
  • Users with JavaScript disabled, or on mobile devices, will very likely not be able to use it.
  • The speed advantages might turn out to be minimal, especially if the user's using a slow JavaScript engine like in IE.
  • Maintainability: Unless you automate the generation of your javascript, it's going to be a nightmare!

All in all

This method is not recommended if you're doing it only for speed increase. However, it is often done in web applications, where users stay on the same page, but then you would probably be better off using one of the existing frameworks for this, such as backbone.js, and make sure it remains crawlable by following Google's hashbang guidelines or HTML5 PushState (as suggested by @rohk).

If it's just performance you're looking for, and your app doesn't strictly need to work like this, don't do it. But if you do go this way, then make sure you do it in the standardized way so that it remains fast and indexable.

孤独陪着我 2024-12-29 09:35:31

客户端模板通常用于单页应用程序

您必须权衡利弊:

优点:

  • 更快
  • 界面响应速度

缺点:

  • SEO 将比经典网站更难(除非您使用 < a href="http://lostechies.com/derickbailey/2011/09/26/seo-and-accessibility-with-html5-pushstate-part-1-introducing-pushstate/" rel="nofollow noreferrer">HTML5 PushState)
  • 辅助功能:您严重依赖 javascript...

如果您要这样做,我建议您查看 Backbone.js
您可以在这里找到教程和示例:http:// www.quora.com/What-are-some-good-resources-for-Backbone-js

SPA 示例:

另请查看此答案:https://stackoverflow.com/a/8372246/467003

Client-side templating is often used in Single Page Applications.

You have to weight the pros and cons:

Pros :

  • More responsive interface
  • Load reduced on your web server

Cons :

  • SEO will be harder than for a classic website (unless you use HTML5 PushState)
  • Accessibility : you are relying heavily on javascript...

If you are going this way I recommend that you look at Backbone.js.
You can find tutorials and examples here : http://www.quora.com/What-are-some-good-resources-for-Backbone-js

Examples of SPA :

Also look at this answer : https://stackoverflow.com/a/8372246/467003

枉心 2024-12-29 09:35:31

哎呀。

jQuery 模板更接近您的想法。 Sanity 说你应该有一些可以轻松随意修改的 HTML 模板。您想要的是可维护性,而不是让您在最糟糕的噩梦中翻来覆去的字符串连接。

你可以继续这种疯狂,但你最终会以艰难的方式学习。我让您首先使用 jQuery 模板和纯代码方式尝试一些原型。我的朋友,理智一定会战胜你,我说这是因为你尝试了一段时间的方式。 :)

也可以使用 AJAX 动态加载您需要的模板内容。如果您拥有一种灵丹妙药,需要在单个请求中下载每个可以想到的模板,这是没有意义的。

Yikes.

jQuery templates are closer to what you are thinking. Sanity says you should have some HTML templates that you can easily modify at will. You want MAINTAINABILITY not string concatenations that keep you tossing and turning in your worst nightmares.

You can continue with this madness but you will end up learning the hard way. I employ you to first try some protypes using jQuery templates and your pure code way. Sanity will surely overcome you my friend and I say that coming from trying your way for a time. :)

Its possible to load content in dynamically of the template you need as well using AJAX. It doesn't make sense that you will have a panacea approach where every single conceivable template is needed to be downloaded in a single request.

乖乖公主 2024-12-29 09:35:31

优点?我实在想不出有什么办法。您说在网络服务器中会更容易,但提供 HTML 正是网络服务器的设计目的。

缺点?在构建网站时,它几乎违背了所有最佳实践:

  • 搜索引擎将无法很好地索引您的网站,如果
  • 可维护性降低的
  • 话,无论 JS 引擎有多快,DOM 都很慢,并且永远不会像就像在服务器上构建 HTML 一样快
  • ,一个 JS 错误就会导致整个网站无法呈现。哎呀。然而,浏览器对 HTML 错误的容忍度极高。

归根结底,HTML 是一个用于显示内容的出色工具。 JavaScript 是添加交互的好方法。像你建议的那样将它们混合起来是疯狂的,只会导致问题。

The pros? I really can't think of any. You say that it will be easier in the webserver, but serving HTML is what web servers are designed to do.

The cons? It goes against just about every best practise when it comes to building websites:

  • search engines will not be able to index your site well, if at all
  • reduced maintainability
  • no matter how fast JS engines are, the DOM is slow, and will never be as fast as building HTML on the server
  • one JS error and your entire site doesn't render. Oops. Browsers however are extremely tolerant of HTML errors.

Ultimately HTML is a great tool for displaying content. JavaScript is a great way of adding interaction. Mixing them up like you suggest is just nuts and will only cause problems.

通知家属抬走 2024-12-29 09:35:31

缺点

  1. SEO
  2. 排除没有 JavaScript 的用户

优点

  1. 更快的工作流程/更流畅的界面
  2. 小负载减少

如果 SEO 对您来说并不重要并且您的大多数用户都使用 Javascript,您可以创建一个 单页应用程序。它不会减少太多负载,但可以在页面上创建更快的工作流程。

顺便说一句:我不会将所有模板打包在一个文件中。对于大型项目来说它太大了。

Cons

  1. SEO
  2. Excluding user without javascript

Pros

  1. Faster workflow / Fluider interface
  2. Small load reduce

If SEO is not important for you and most of your user have Javascript you could create a Single Page Applications. It will not reduce your load very much but it can create a faster workflow on the page.

Btw: I would not pack all templates in one file. It would be too big for big projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文