问题
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?
回答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."
回答2:
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...)
来源:https://stackoverflow.com/questions/7372345/what-does-firefox-decide-not-to-cache