简体   繁体   English

使用大量HTML呈现页面

[英]Rendering page with HUGE amount of HTML

I'm creating a website that has a huge amount of HTML (many thousands of div elements). 我正在创建一个拥有大量HTML(数千个div元素)的网站。 There's no real way to get away from having all these div elements and it's making the site load very slow (7-12 seconds). 没有真正的方法可以摆脱所有这些div元素,并且它使网站加载非常慢(7-12秒)。 I've tried putting caching on the site, but it doesn't help, since the site still has to render all these div elements. 我已经尝试在网站上放置缓存,但它没有帮助,因为该网站仍然必须渲染所有这些div元素。

More specifically it's 140 dropdowns, that each contain 100-800 div elements and they take a long time to show. 更具体地说,它是140个下拉列表,每个下拉列表包含100-800个div元素,它们需要很长时间才能显示出来。

My thoughts, was to render the div elements that are inside the dropdowns, after the page loads, but I don't know how to go about that? 我的想法是,在页面加载后渲染下拉列表中的div元素,但我不知道如何去做?

What is the easiest way to render some of your partials AFTER the page has loaded? 在页面加载后渲染部分内容的最简单方法是什么? I'm using Rails 4 btw. 我正在使用Rails 4顺便说一句。

Any other suggestions on how to deal with HUGE amounts of HTML? 关于如何处理大量HTML的任何其他建议?

I have a similar issue on one of my pages. 我的一个页面上有类似的问题。 Here are some things to try related to the select boxes. 以下是与选择框相关的一些尝试。

(The top two may not be relevant since you said you tried caching, but I'm including for completeness. What type of caching did you try? How did you verify it was the browser rendering that was slow?) (前两个可能不相关,因为你说你尝试过缓存,但我包括完整性。你尝试过什么类型的缓存?你是如何验证浏览器渲染速度慢的?)

Double check the cause of the problem 仔细检查问题的原因

Comment out the code that generates the select boxes and check whether the time in your rails logs (as opposed to your browser measurements) drops. 注释掉生成选择框的代码,并检查rails日志中的时间(与浏览器测量值相反)是否下降。 This establishes your "lower bound" for performance improvements on that measure. 这确定了您对该度量的性能改进的“下限”。

Avoid using rails helpers. 避免使用铁轨助手。

If you're using select_tag , options_for_select , or any of that family of methods you may be doing a lot of repeated work since each time they are called they need to rebuild the list of options. 如果您正在使用select_tagoptions_for_select或任何一系列方法,那么您每次调用它们时可能会进行大量重复工作,因此需要重建选项列表。 If the options are the same for each select box, you can build them up once then just dump them in the page: 如果每个选择框的选项相同,则可以将它们构建一次,然后将它们转储到页面中:

<% options = options_from_collection_for_select(@array, "id", "name") %>
<%= select_tag "myfield", options %>

If you need different selected values for each, you can try: 如果您需要为每个选择不同的值,您可以尝试:

  1. Processing options after creation to add them. 创建后处理options以添加它们。 This is pretty gross and possibly won't give you much speed up over the default generators. 这非常严重,可能不会比默认生成器更快。
  2. Dump the defaults into a javascript variable, then set them with JS after the page loads. 将默认值转储为javascript变量,然后在页面加载后使用JS设置它们。

AJAX in partials 部分中的AJAX

This will give the illusion of loading faster, even though server time is the same (though it may be parallelized somewhat, you add extra network delay). 即使服务器时间相同,这也会给出更快加载的错觉(虽然它可能会稍微并行化,但会增加额外的网络延迟)。 The easiest way to do this is with jQuery's .load method . 最简单的方法是使用jQuery的.load方法

$("#element-1").load("/path/to/partial/1")

Generate select boxes in JS 在JS中生成选择框

If you can get all the data you need to the client relatively fast (maybe serve it up in a JSON endpoint?) you can try building up the select boxes directly with jQuery, which is covered in Programmatically create select list 如果您可以相对快速地获得客户端所需的所有数据(可能在JSON端点中提供?),您可以尝试使用jQuery直接构建选择框,这在编程创建选择列表中有所介绍

Remove redundant HTML 删除多余的HTML

Why do your dropdowns have div s inside them? 为什么你的下拉列表里面有div Is there a way to simplify? 有简化方法吗?

Although the question is few years old, maybe someone will benefit from this one. 虽然这个问题已有几年历史,但也许有人会从中受益。

1) in controller: 1)在控制器中:

@companies = Company.all.pluck(:name, :id) #order is important here

2) in view: 2)在视图中:

<%= f.select(:company_id, options_for_select(
             @companies, selected: @user.company_id
             ), {}, class:"form-control m-b") %>

pluck grabs only :name, :id values, array of arrays, eg, pluck grabs only :name, :id values,array array,例如,

=> [["Sumptus xiphias canto.", 5], ["My First Co.", 1]]

is created and then options_for_select populates options. 已创建,然后options_for_select填充选项。

My 5000 records are populated in ~300ms in AJAX modal window. 我的5000条记录在AJAX模态窗口中填充~300ms。 Not super fast, however I don't think regular user would complain. 不是超级快,但我不认为普通用户会抱怨。

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

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