Reputation: 359
I have a rails application that is going to be accessed heavily from mobile devices so I am looking into aggressively caching assets on the device. One thing I am running into is the issue of using caching with the asset pipeline enabled.
If I set the :cache option to true on my stylesheet_link_tag I run into an issue where the server can't find the application.css file which contains the *=require_tree . directive that loads all of my css files.
No such file or directory - Asset file not found at '.../public/stylesheets/application.css'
I am researching the issue and I am seeing a lot of stuff that says you should be serving static assets rather than using the asset pipeline if you are interested in caching your css/js files. That's well and good but I hear so much about how awesome the asset pipeline is and I like the minification aspects it provides.
My question is this: Is there a way to take advantage of the minification aspects of the asset pipeline while also being able to have the browser cache my css/js files?
Upvotes: 0
Views: 187
Reputation: 10630
Yes. You should not use cache: true in your js and css tag declarations. In production mode, you should precompile assets and those assets will be served by your webserver. Compiled asset will have fingerprint in the end so in future if you update asset, its fingerprint will change, thus filename will change and you are guaranteed that browser will get new file and thus you can configure your webserver to use aggressive caching methods for assets. Please read rails guide for assets to understand exactly how assets work. If you scroll down to 4.1.1 section you will see following text:
4.1.1 Far-future Expires Header
Precompiled assets exist on the filesystem and are served directly by your web server. They do not have far-future headers by default, so to get the benefit of fingerprinting you'll have to update your server configuration to add them.
For Apache:
# The Expires* directives requires the Apache module `mod_expires` to be enabled.
<Location /assets/>
# Use of ETag is discouraged when Last-Modified is present
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
</Location>
Upvotes: 1