In rails, js only merges without compression in production, and assets pipeline loads js on specified pages.

Source: Internet
Author: User

Because I just learned rails, I tried to create a small system to operate a public account,

After the deployment, I encountered a problem. The whole system was very slow to access on the Internet, but the access on the mobile phone was 10 seconds later,

Open the chrome debugging tool and find that the application-(xxxxdigestxxxxxx). js file contains more than 800 k

Open this file and you will find that there are uncompressed jquery comments and Code for the re-control ckeditor.

So the problem becomes

1. How to dewatermark jquery

2. Load other heavy controls, such as ckeditor js (500 k), as needed)

 

1 is actually a mistake made by myself, because I didn't bring the correct production parameters during pre-compilation.

Rake assets: precompile RAILS_ENV = production

Why didn't I write it like this at the beginning? Because I added this parameter at the beginning, but I found that the terminal was "stuck" when it was added, so ctrl + c stopped the execution.

Later, it was found that, instead of getting stuck, some JavaScript code with heavy controls was complicated, and node. js is still performing operations. For example, it takes about 3 minutes for ckeditor to execute precompile in my notebook.

 

We all know that application. js is referenced in layouts/application.html. erb, and application. js contains a sentence by default.

// = Require tree. The meaning of this sentence is to add all js files under the Directory

If we want to load js separately, we must first kill this sentence.

Then, you need to reserve a yield position for the header in layouts/application.html. erb to load your js Code on the specified page.

I added <% = yield: head %> after <% = csrf_meta_tags %>.

Then load the specific js on the specific page. Load the corresponding js on the specified page. For example, here

<% Content_for: head do %>

<% = Javascript_include_tag "ckeditor_load", "data-turbolinks-track" => true %>

<% End %>

 

 

Of course, you need to write the ckeditor_load.js file in app/assets/javascripts. Note that you need to add the precompile compilation range to config/application. rb.

 

Config. assets. precompile + = ['admin. js', 'ckeditor _ load. js', 'swfobject. js']

 

Finally, rake assets: precompile RAILS_ENV = production

In this way, js classification can be used for loading, JavaScript compression, and deletion of comments and other information. application. js starts from 800 KB and ends at around KB of the general page, which is more scientific.

 

References

Http://guides.rubyonrails.org/asset_pipeline.html

Http://chloerei.com/2013/03/10/rails-assets-pipeline-s-value/

Here is a little bit. After I posted a message on rei's blog, rei almost immediately replied. Later I thought the blogger's profile was very familiar and finally remembered it, this guy is ruby-china's first member (http://ruby-china.org/rei), suddenly scared.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.