hyperrjas
hyperrjas

Reputation: 10744

ruby on rails 3.1 leverage browser caching

I have run in production mode my smart aplication in rails 3.1 with mongodb.

I can see that page speed from firebug have a smart problem:

leverage browser caching of static, you can see in the above image:

enter image description here

I have check this fix but not working for me:

ROR + MVC Disable Browser Cache

I want to know how can I fix this problem for my image, css and javascript, or if there is a gem for this.

Upvotes: 4

Views: 2828

Answers (2)

hyperrjas
hyperrjas

Reputation: 10744

Thank you for their response Hightechrider and miaout17.

Im using apache. I add to apache2.conf the next code:

<LocationMatch "^/assets/.*$">
 # Some browsers still send conditional-GET requests if there's a
 # Last-Modified header or an ETag header even if they haven't
 # reached the expiry date sent in the Expires header.
 Header unset Last-Modified
 Header unset ETag
 FileETag None
 # RFC says only cache for 1 year
 ExpiresActive On
 ExpiresDefault "access plus 1 year"
</LocationMatch>

Bue I get the next error when I try reset my server apache:

Syntax error on line 256 of /etc/apache2/apache2.conf: Invalid command 'ExpiresActive', perhaps misspelled or defined by a module not included in the server configuration Action 'restart' failed. The Apache error log may have more information.

Some idea?

Edited I found the fix for this error:

If you have a ubuntu user You can take a shortcut using a2enmod, a command that enables the module for you automatically:

sudo a2enmod headers

and then restart your server and voila :D working fine.

Upvotes: 7

miaout17
miaout17

Reputation: 4875

According to Asset Pipeline Guides, you need to set expiration in your web server configuration file. There are example configuration for Apache and Nginx in the guide, please read section 4.1.1 "Server Configuration" and try it.

ROR + MVC Disable Browser Cache doesn't work because you are hosting static files with web server, so the request won't go through the controller.

I think my previous answer is wrong because Expiration not specified might means browser won't cache anything, and cause very bad performance.


Previous (wrong) answer:

In fact, it won't cause any problem. You need set an expiration time if your file needs expire. However, in Rails 3.1, asset pipeline will automatically add MD5 to the file name. If the file is changed, the URL will also be changed. Thus it's safe not to set expiration time.

Upvotes: 3

Related Questions