0

I'm viewing the network traffic from my rails application using firebug and I see that the css and javascript files are not being cached. In the page, are things like:

<script src="/javascripts/prototype.js?1315256241" type="text/javascript"></script>

and it appears as if the ?1315256241 causes FF to not cache the item. But the ?1315256241 is a "feature" of Rails that was introduced a few years back. So I'm surprised that it prevents the item from being cached yet no one has asked about it or fixed it.

I've ask this same question on the FF forum and maybe in the Rails forum to no avail. No one seems to understand my question or the point of it. So I thought I would try here.

My question is "does the ?1315256241 suffix (the query) prevent FF from caching the page?"

And if it does, what do most people do in Rails to prevent that?

pedz
  • 2,271
  • 1
  • 17
  • 20

2 Answers2

1

Am I right in thinking that the number after the ? is a random generated number? This method is sometimes used crudely to prevent caching

If this is the case then Firefox will not cache as each time it thinks it is requesting a different file see below for example.

/javascripts/prototype.js?1315256241

/javascripts/prototype.js?1315256242

/javascripts/prototype.js?1315256243

/javascripts/prototype.js?1315256244

etc etc

I'd look at removing the number in the query string as It doesn't look to be required. I'm not a rails dev, so would be interested to see what you mean about this being a "Feature."

Community
  • 1
  • 1
LiamB
  • 18,243
  • 19
  • 75
  • 116
  • It is not random but a timestamp of when it changed last. When I refresh, the number doesn't change. – pedz Sep 10 '11 at 16:11
  • So why is the time-stamp being added? It's not part of the Filename. – LiamB Sep 10 '11 at 16:13
  • Thank you! In answering your question, I found the answer to the larger question. Here is a link to the appropriate page (for others to find). http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html Look for "Customizing the asset path" The short answer is I need to add some stuff to my Apache config to take advantage of this. – pedz Sep 11 '11 at 02:33
  • Interesting! Will be useful all round. Glad your issue is solved! – LiamB Sep 11 '11 at 12:29
0

If you view the documentation for the AssetTagHelper and look for "Customizing the asset path" you will find this explanation of why the code adds the timestamp along with the needed changes to my Apache configuration to take advantage of this feature.

By default, Rails appends asset’s timestamps to all asset paths. This allows you to set a cache-expiration date for the asset far into the future, but still be able to instantly invalidate it by simply updating the file (and hence updating the timestamp, which then updates the URL as the timestamp is part of that, which in turn busts the cache).

It’s the responsibility of the web server you use to set the far-future expiration date on cache assets that you need to take advantage of this feature. Here’s an example for Apache:

# Asset Expiration
ExpiresActive On
<FilesMatch "\.(ico|gif|jpe?g|png|js|css)$">
  ExpiresDefault "access plus 1 year"
</FilesMatch>

(and the documentation continues...)

pedz
  • 2,271
  • 1
  • 17
  • 20