Forum: Ruby on Rails Browser caching of javascript files

0ce4db5e1083663c575158643313852c?d=identicon&s=25 Perry Smith (pedzsan)
on 2011-08-07 19:49
I'm using the latest Firefox and firebug with Rails 2.3.11 (and Ruby
1.9.1).

I see my javascript assets have the last modified date, such as:

    javascripts/prototype.js?123456789

which has been the case for a while.  What I just noticed is that
Firefox (according to Firebug) is fetching this for each request.  This
is for a Rails server running in "production" mode.  The browser's
caching settings are normal (true for things like
browser.cache.disk.enable and browser.cache.memory.eable).

With my super small internet connection, pulling over prototype,
scriptaculous, and all its friends is a significant hit -- plus, it just
doesn't make any sense for it to do it every time.

The underlying environment is Apache and Thin (both relatively recent).
It seems like either the browser should not attempt to fetch it at all
(which it tends to do if the ?123456789 isn't there) or the server
should be returning 304 (not modified).

Is this normal?  What options do I have to change this?

Thank you,
pedz
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2011-08-07 20:42
(Received via mailing list)
On Aug 7, 6:49pm, Perry Smith <li...@ruby-forum.com> wrote:
> caching settings are normal (true for things like
>
> Is this normal? What options do I have to change this?

You're actually getting a 200 request back with the full file?

If you want the browser not to bother even fetching the file then you
need to set headers saying that content can be cached.

Something like

<Location /javascripts>
    ExpiresActive on
    ExpiresDefault "access plus 21 days"
</Location>

should tell browsers that they can cache stuff from javascripts for up
to 21 days.
For stuff like prototype, scriptaculous etc you might want to consider
using the copies hosted by ( http://code.google.com/apis/libraries/ ).
That way those files come from google's network rather than yours and
they take care of setting all the right caching headers (as well as
having a cdn that is spread worldwide)

Fred


Fred
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.