Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Sprockets asset caching in development

I'm using Rails 3.2.13 and the Rails Asset Pipeline. I want to use the Asset Pipeline so I can use SASS and CoffeeScript and ERB for my assets and have the Pipeline automatically compile them, so I cannot turn off the pipeline in development. I am not precompiling assets in development ever and there is not even a public/assets/ directory.

However, when I make changes to an included file, such as to a _partial.html.erb file that is included (rendered) in a layout.html.erb file, without changing the file doing the including itself (in this example layout.html.erb), Sprockets doesn't detect the change and invalidate the cache, so I keep getting the same stale file. When I'm doing this in active development, I want to disable any caching of assets so I can get the changes on every request but I cannot figure out how to do this. I have set all of the following in my development.rb:

config.action_controller.perform_caching = false
config.action_dispatch.rack_cache =  nil
config.middleware.delete Rack::Cache
config.assets.debug = true
config.assets.compress = false
config.cache_classes = false

Still, even with this, files show up under tmp/cache/assets/ and tmp/cache/sass/ and changes are not available on future requests. Right now I have to manually delete those directories every time I want to see a change.

Unfortunately, the entire contents of the How Caching Works section of the RoR Guide for the Asset Pipeline is:

Sprockets uses the default Rails cache store to cache assets in development and production.

TODO: Add more about changing the default store.

So, how can I get Sprockets to compile assets on demand but not cache the results?

like image 838
Old Pro Avatar asked Jun 06 '13 19:06

Old Pro


People also ask

What is asset caching?

Caching is a simple concept. When the browser downloads an asset, it follows a policy dictated by the server to figure out whether or not it should download that asset again on future visits. If a policy isn't defined by the server, browser defaults will kick in, which is usually to cache files for that session.

How do you Precompile an asset?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Precompile do?

RAILS_ENV=production tells Rails to compile the production version of the assets. assets:precompile is a Rails provided rake task that has instructions for compiling the assets.


1 Answers

Here's the magic incantation:

config.assets.cache_store = :null_store  # Disables the Asset cache
config.sass.cache = false  # Disable the SASS compiler cache

The asset pipeline has it's own instance of a cache and setting config.assets.cache = false does nothing, so you have to set its cache to be the null_store to disable it.

Even then, the SASS compiler has it's own cache, and if you need to disable it, you have to disable it separately.

like image 68
Old Pro Avatar answered Oct 06 '22 01:10

Old Pro