Disable Sprockets Asset Caching in Development

Disable Sprockets asset caching in development

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.

Disable Sprockets asset caching in development on Rails 4

If you look at the Sprockets source, you can see that if cache_classes is true then app.assets gets set to app.assets.index, and the filesystem is no longer checked.

In order to get around this in development, you can add something similar to the following to your development.rb configuration:

# Sprockets configuration: prevent sprockets from caching assets in development
# when cache_classes is set to true
sprockets_env = nil
config.assets.configure do |env|
sprockets_env = env

# Sprockets environment configuration goes here
# env.js_compressor = :uglifier # or :closure, :yui
# env.css_compressor = :sass # or :yui
end

if config.cache_classes
config.after_initialize do
Rails.application.assets = sprockets_env
end
end

This essentially grabs a reverence to the Sprockets::Environment object before it is overwritten by the Sprockets::Index one, and allows the filesystem to be checked for new assets even when cache_classes is true. This seems to work for us in development, so hopefully it helps someone else out as well.

Disable the sprockets assets caching in rails 5+

As per the Rails docs, add the following code block to your environments/development.rb

config.assets.configure do |env|
env.cache = ActiveSupport::Cache.lookup_store(:null_store)
end

Prevent Rails 4 Sprocket Asset Pipeline from caching a specific file

As I understood, you need always to recompile .erb assets. Here is the solution:

Sprockets::Asset.class_eval do
alias_method :orig_dependency_fresh?, :dependency_fresh?
def dependency_fresh?(environment, dep)
if dep.pathname.extname.eql? '.erb'
false
else
orig_dependency_fresh?(environment, dep)
end
end
end

Prevent Rails 3.2.11 asset pipeline from caching?

To turn off the asset cache:

config.assets.cache_store = :null_store

Note that that is config.assets.cache_store not the Rails config.cache_store.

Also note that Sass has a separate cache for compiled stylesheets, by default in tmp/cache/sass, and if you want to disable that you have to do that separately:

config.sass.cache = false

To answer the bonus question, when the Rails Guide says:

The default Rails cache store will be used by Sprockets to cache assets in development and production.

I thought they meant the configured Rails cache store would be used. I was wrong, it uses the default cache unless you explicitly change the asset cache.

Detecting stale asset with sprockets?

I can't answer how to get around this, but I can (with some confidence) tell you why its behaving as it is. Sprockets doesn't only mtime to determine staleness, it uses a digest of the file itself. It will return the asset as fresh first if the mtime hasn't been recently updated, and secondly if the hash digest is unchanged (for the relevant method, look here for dependency_fresh?). Since a touch won't change the hash of a file, Sprockets will consider it fresh.

I don't know quite what you're goal is here, so I can't give much advice here. The dependency tracking that is used is mostly private, but it would be possible to hack around this to force a reset. If what you're looking for is a quick way to force an asset to be stale for the sake of local testing while developing a gem, you may consider creating a monkey patch for Asset or ProcessedAsset that can flush the old mtime and digest values.


EDIT - I've done a bit more digging, and I think I found some useful things. The index method on assets creates a new object on each call, and is in effect a snapshot of the assets at the time, while the environment will constantly refresh when you ask it for an asset - looking up an asset causes it to automatically refresh that asset if it is stale.

In theory, this should have a surprisingly easy solution - just invert your fresh calls to x['path/to/asset'].fresh?(e).

how to prevent sprockets from caching a .erb file?

You could separate your JavaScript into libraries (.js.erb or just .js) and configuration data (such as your var foo). Then leave all the library code in the hands of Sprocket and put your configuration into your normal ERB views (probably embedded in your layouts).

You could also serve the configuration data through a separate controller (/config.js perhaps) if that fits your architecture better.

This approach avoids your whole problem by separating static libraries from non-static data. Also, this approach fits nicely with the Rails 3.1 asset pipeline where you're supposed to pre-compile everything before your production deployments.

How to disable adding .self. in Sprockets 3.0

In Sprockets 3, .self.css is added because you have the config.assets.debug = true config set (not the digest config, that's unrelated).

If you add the following to your development.rb or production.rb file, it will work as you expect:

config.assets.debug = false


Related Topics



Leave a reply



Submit