2015-06-10 43 views
2

我正在rails上使用ruby開發一個網頁。 我發現我的一個網頁加載時間很長。 我使用Pingdom的測試吧,這裏是結果: enter image description hereWeb頁面加載時間爲Rails網頁

然後我指的是這和YSlow的報告

一些F級選項:

1.Grade F於減少HTTP請求 此頁面有78個外部Javascript腳本。嘗試將它們合併爲一個。 減少頁面上組件的數量可減少呈現頁面所需的HTTP請求數量,從而加快頁面加載速度。減少組件數量的一些方法包括:合併文件,將多個腳本合併成一個腳本,將多個CSS文件合併到一個樣式表中,並使用CSS Sprites和圖像映射。

2.添加過期F級標題 有82個靜態組件沒有遠期過期日期。

3.Grade F於使用無cookie的域 但是也有一些不是免費的餅乾

4.Grade F於把在JavaScript的底部 81個組件有在的頭部發現有78個JavaScript腳本的文件

我覺得主要是混亂的java腳本或CSS造成漫長的等待時間。 建議在頁面末尾放置java腳本。

的問題是:對於Ruby on Rails的,我的JavaScript文件:應用程序\資產\ JavaScript的\ custom.js ,而我的看法頁面中:應用程序/視圖/ sth.html.erb 我不知道如何把它放在最後。

附上我在app /視圖代碼/ sth.html.erb

<h3>Project Information</h3> 
<div class="project-report"> 
    <table class="content-table"> 
    <tr> 
     <td><strong>PJO Number</strong></td> 
     <td><%= @project.job_number %></td> 
    </tr> 
    <tr> 
     <td><strong>Client</strong></td> 
     <td><%= @project.client_name %></td> 
    </tr> 
    <tr> 
     <td><strong>Location</strong></td> 
     <td><%= @project.jobsite_location %></td> 
    </tr> 

    <br> 
    <td> 
    <tr> 
     <td><strong>Contacts</strong></td> 

    </tr> 
     <br> 
     <table class="yellow-header-table contact-table"> 
      <tr> 
      <th>Name</th> 
      <th>Designation</th> 
      <th>Email</th> 
      <th>SMS</th> 
      <th>Fax</th> 
      </tr> 
      <% @project.contacts.each do |contact| %> 
      <tr> 
      <td><%= contact.contact_person_name %></td> 
      <td><%= contact.designation %></td> 
      <td><%= contact.email %></td> 
      <td><%= contact.phone_number %></td> 
      <td><%= contact.fax_number %></td> 
      </tr> 
      <% end %> 
     </table> 

    </td> 
    </table> 

    <table class="yellow-header-table"> 
    <tr> 
     <th>Instrument Type</th> 
     <th>Total</th> 
     <th>Installed</th> 
     <th>Active</th> 
     <th>Remarks</th> 
    </tr> 
    <tr> 
     <td>first Meter</td> 
     <td><%= @project.quantity_active %></td> 
     <td><%= @project.quantity_active %></td> 
     <td><%= @project.quantity_active %></td> 
     <td><%= @project.sound_remarks %></td> 
    </tr> 

    </table> 
<div style='margin-left:50px;float:left;'> 
    <button id="buttonN" class="btn btn-info" style="width:190px;">View Data1</button> 
    <button id="buttonV" class="btn btn-info" style="margin-left:50px;width:190px;">View Data2</button> 
    <button id="buttonR" class="btn btn-info" style="margin-left:50px;width:190px;">Generate Report</button> 
</div> 





<div id="report-reviewn" class="hidden" style='margin-top:55px;'> 
<% if (@devices.count > 0) %> 
    <h4 style="width:100%">1st Devices</h4> 

    <table id="tableselect" class="yellow-header-table"> 
    <tr> 
     <th scope="Row">Select</th> 
     <th>Device ID</th> 
     <th>Brand</th> 
     <th>Address</th> 
    </tr> 



    <%= form_tag viewdata_devices_path,class:"form-horizontal viewlist-form" do %> 
    <% @project.first_devices.each do |first_device| %> 
    <tr> 
     <th scope='col'> 
     <%= radio_button_tag(:deviceID, first_device.id) %></th> 
     <td><%= first_device.device_id %></td> 
     <td><%= first_device.serial_number %></td> 
     <td><%= first_device.brand %></td> 
     <td><%= first_device.last_calibration_date %></td> 
     <td><%= first_device.remarks %></td> 
     <td><%= @project.project_type %></td> 
     <td> 
     <select style='border:none;'> 
     <option><%= first_device.device_location %></option> 
     <option><%= first_device.device_latlng %></option> 
     </select> 
    </td> 
    </tr> 
    </tr> 
    <% end %> 
    </table> 

     <table class="table-row"> 

     <tr> 

     <th> <%= text_field_tag 'startday', nil, placeholder: 'Start Date',style:'width:100px;', class: "date-picker" %></th> 

     <th> <%= text_field_tag 'endday', nil, placeholder: 'End Date' ,style:'width:100px;',class: "date-picker" %><th> 
      <%= hidden_field_tag "project_id", @project.id %> 

     <th> <%= submit_tag "View Data",class: "btn" %></th> 

    <% end %> 
     </tr> 
     </table> 



    <% else %> 
    <p style="width:100%;text-align:left;margin-left:50px;">There is no first device for this project </p> 
    <% end %> 

</div> 










    <div id="report-reviewv" class="hidden" style='margin-top:55px;' > 
     <% if (@vdevices.count > 0) %> 

    <table class="yellow-header-table"> 
    <h4 style="width:100%">second Devices</h4> 
    <tr> 
     <th scope="Row">Select</th> 
     <th>Device ID</th> 
     <th>Address</th> 
    </tr> 

    <%= form_tag viewdata_second_devices_path,class:"form-horizontal viewlist-form" do %> 
    <% @project.second_devices.each do |second_device| %> 
    <tr> 
     <th scope='col'> 
     <%= radio_button_tag(:deviceID, second_device.id) %></th> 
     <td><%= second_device.device_id %></td> 
     <td><%= second_device.serial_number %></td> 
     <td><%= second_device.brand %></td> 
     <td><%= second_device.current_trigger_value %></td> 
     <td><%= second_device.last_calibration_date %></td> 
     <td><%= second_device.remarks %></td> 
     <td><%= @project.project_type %></td> 
     <td> 
     <select style='border:none;'> 
     <option><%= second_device.device_location %></option> 
     <option><%= second_device.device_latlng %></option> 
     </select> 
    </td> 
    </tr> 
    <% end %> 

    </table> 

     <table class="table-row"> 

     <tr> 

     <th> <%= text_field_tag 'startday', nil, placeholder: 'Start Date',style:'width:100px;', class: "date-picker" %></th> 

     <th> <%= text_field_tag 'endday', nil, placeholder: 'End Date' ,style:'width:100px;',class: "date-picker" %><th> 
     <%= hidden_field_tag "project_id", @project.id %> 
     <th> <%= submit_tag "View Data",class: "btn" %></th> 

    <% end %> 
     </tr> 
     </table> 

    <% else %> 
    <p style="width:100%;text-align:left;margin-left:50px;">There is no second device for this project </p> 
    <% end %> 
</div> 




    <div id="report-reviewr" class="hidden" style='margin-top:100px;'> 

    <div class="report-forms report-left-align"> 
    <span><strong>Report Type</strong></span> 
    <%= select_tag "report-type", options_for_select([["first", "first-report"], ["second", "second-report"]]), :prompt => "Choose Report" %> 

    <div id="first-report" class="report-form hidden"> 
     <%= form_tag report_first_devices_path, class: "form-horizontal device-form", target: "_blank" do %> 
     <div class="control-group"> 
     <%= label_tag "Choose Devices", nil, class: "control-label" %> 
     <div class="controls"> 
      <% selected_first_device = @project.first_devices.blank? ? nil : @project.first_devices.first.id %> 
      <%= select_tag "devices", options_from_collection_for_select(@project.first_devices, "id", "serial_number", selected_first_device), :multiple => false %> 
     </div> 
     </div> 

     <div class="control-group"> 
     <%= label_tag "Choose Time", nil, class: "control-label" %> 
     <div class="controls"> 
      <%= text_field_tag "time_range", nil, class: "input-large date-picker" %> 
     </div> 
     </div> 

     <%= hidden_field_tag "project_id", @project.id %> 
     <%= hidden_field_tag "device_type", "first" %> 

     <div class="control-group"> 
     <div class="controls"> 
      <%= submit_tag "Generate Report", class: "btn" %> 
     </div> 
     </div> 
     <% end %> 
    </div> 

    <div id="second-report" class="report-form hidden"> 
     <%= form_tag report_second_devices_path, class: "form-horizontal device-form", target: "_blank" do %> 
     <div class="control-group"> 
     <%= label_tag "Choose Devices", nil, class: "control-label" %> 
     <div class="controls"> 
      <% selected_second_device = @project.second_devices.blank? ? nil : @project.second_devices.first.id %> 
      <%= select_tag "devices", options_from_collection_for_select(@project.second_devices, "id", "serial_number", selected_second_device), :multiple => false %> 
     </div> 
     </div> 

     <div class="control-group"> 
     <%= label_tag "Choose Time", nil, class: "control-label" %> 
     <div class="controls"> 
      <%= text_field_tag "time_range", nil, class: "input-large date-picker" %> 
     </div> 
     </div> 

     <%= hidden_field_tag "project_id", @project.id %> 
     <%= hidden_field_tag "device_type", "second" %> 

     <div class="control-group"> 
     <div class="controls"> 
      <%= submit_tag "Generate Report", class: "btn" %> 
     </div> 
     </div> 
     <% end %> 
    </div> 
    </div> 



    </div> 

</div> 

任何人都可以給一些建議?

回答

2

在默認的app/views/layouts/目錄中應該有一個名爲application.html.erb的文件。該文件包含頭

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 

內以下行如果將這個剛剛閉幕body標籤下面所有的JS會在頁面上的每個頁面加載底部。這是不阻止頁面客戶端呈現的最有效方法。

最後將通過編譯application.js包含所有文件。

編輯1

,正如你在YSlow的結果所看到的,你有很多的 - 與我的意思是很多JS的。所有這些文件執行dns lookup(它們將被緩存但仍然頻繁更新)。每個n的dns查找都會帶來懲罰,因爲緩存的dns查找只存在於某天的某個事件中。減少請求數量是使應用程序更快速的關鍵。

你可能想看看這些有關資產的管道和/或做什麼來壓縮你的資產的額外信息。

+0

我已經移動了這條線,但等待時間仍然在10秒左右。我是否需要重新啓動以獲得此功能?或者可能是其他原因造成這個問題:做少量http請求/添加過期標題/縮小Javascript和CSS – Orz

+0

是啊看到JS的名單嚇了我一跳,我不知道爲什麼我沒有把它放在我的答案。我會添加一些信息。 – SidOfc