how does rails expire assets?
-
12-11-2019 - |
Question
In application.rb
, there is the following:
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
I've incremented this to 1.1, but when I reloaded my page live, the old js was still loaded. I looked at the source, expecting to find a GET version parameter appended to the js and css files, but there was no such thing.
<link href="/assets/application.css" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/application.js" type="text/javascript"></script>
What forces the browser to clear its cache?
Solution
The version number is only taken into account when creating the md5 fingerprints for your assets.
OTHER TIPS
Set:
config.assets.digest = true
in production.rb. Then when you precompile your stylesheets using:
rake assets:precompile
you'll see the fingerprint has been added to the actual filename, like this:
styles-5b4f282be3720d5b1e40d9e343dfc60a.css
This fingerprint is changed whenever the stylesheet (or other asset) is edited. The changed filename forces the browser to clear it's cache.
You have to be using the asset pipeline. Then include the assets with either asset_path, javascript_include_tag etc. Your get doesn't have the Md5 fingerprint, so either your asset pipeline is not enabled, or you aren't linking to your js correctly.