📜 ⬆️ ⬇️

3 simple tips that will make your Rails application faster, part # 1

I know that a lot of people have already written guides to help your web application run faster. But I will try to focus on the simplest, but most effective methods that will help you significantly speed up your application without losing any functionality from Ruby on Rails.
Tip # 1: Clean up your static content.
Tip # 2: Remove all unnecessary
Tip # 3: Cache the whole page

Tip # 1: Clean up your static content.
It often happens that one web application loads several javascripts and css styles at once. This significantly slows down the page loading as the web browser opens a new connection each time for a new file.
The solution is to reduce the number of external resources of your page by combining them all into one file. The AssetPackager plugin will help us in this .

We put:
script/plugin install git://github.com/sbecker/asset_packager.git

Prmer config / asset_packages.yml:
---
javascripts:
- base:
- prototype
- effects
- controls
- dragdrop
- application
- secondary:
- foo
- bar
stylesheets:
- base:
- screen
- header
- secondary:
- foo
- bar

And run rake task:
rake asset:packager:build_all

Further for javascript we write
<%= javascript_include_merged :base %>

<%= javascript_include_merged 'prototype', 'effects', 'controls', 'dragdrop', 'application' %>

For styles we write:
<%= stylesheet_link_merged :base %>

<%= stylesheet_link_merged 'screen', 'header' %>

As a result, for the development mode we get our old code for example:






<link href="/stylesheets/screen.css" type="text/css" />
<link href="/stylesheets/header.css" type="text/css" />

And in the production mode will be:


<link href="/stylesheets/base_packaged.css?123456789" type="text/css" />

Now, to make the load even less, we transfer all of our static files to another host (why here ) On the rails, it is very simple to do, just add the following line to config / environments / production.rb:
config.action_controller.asset_host = "http://assets.example.ru"

Now everything is image_tag, javascript_include_tag, etc. will point to this host.

UPD : Instead of the plugin, you can use <% = javascript_include_tag: all,: cache => true%>, more info here . Thank you grossu

')

Source: https://habr.com/ru/post/50840/


All Articles