1

I have just put into production one of my Ruby On Rails Application and noticed it was really slow in its loading time.

My Application runs on railsplayground.net using passenger on an apache server.

I took a look at a couple of sites including: pingdom , webpagetest and the major downfall I had noticed in my application is that the caching isn't working on my javascript and css files as can be seen here: enter image description here enter image description here enter image description here

I have tried reading all sorts of guides on how to cache, but all of my efforts turned up with no results.

I put this in my header file:

  <meta http-equiv="cache-control" content="public" />
  <meta http-equiv="last-modified" content="Sun, 23 Sep 2012 08:00:00 GMT" />

and this is turned on in my production.rb:

config.action_controller.perform_caching = true

1 Answer 1

4

What these items are complaining about is that the when you are serving static assets (images, css, javascript) the webserver isn't telling the browser that they are cacheable so they will be refetched more often than necessary.

When using the asset pipeline (as you seem to be) the asset filename includes an md5 checksum of its content so you can set very long expiry times: if you change the file then the checksum will change to and so the file browsers requests will have a different name.

You usually do this in apache's configuration, by sticking something like this in the block for the virtual host

<Location /assets>
      ExpiresActive on
      ExpiresDefault "access plus 1000 days"
</Location>

It's also worth turning on gzipping of javascript & css files which can save a lot of time. You'd do this by adding something like this to the virtual host:

<Location />
  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-javascript text/css  application/javascript application/json text/javascript
</Location>

Older browsers may not support this, but this is so widely supported that I turn this on systematically (to give you an idea, even IE 4 and Netscape Navigator 4 support this)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Frederick Cheung <Location /assets> ExpiresActive on ExpiresDefault "access plus 1000 days" </Location> seems to cache the files in public/assets folder of rails app Could you explain how we can cache something outside of assets folder eg : public/system In my app images are uploaded to public/system folder
It really depends how those files are being served - probably deserves a question of its own (assuming there's not already some questions around this)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.