Disable the sprockets assets caching in rails 5+ - ruby-on-rails

I have tried a lot to disable sprockets asset cache in rails but no vain. I have tried to configure development.rb but it is not working at at all.
I am using this code to disable cache generation
config.assets.cache_store = :null_store # Disables the Asset cache
config.sass.cache = false # Disable the SASS compiler cache
ruby version=2.3.3
rails version=5.0.1
thanks in advance.

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

Related

Understanding Rails config.assets - when assets are not enabled

Hopefully this is a really simple question for someone…
If…
rails.config.assets.enabled = false
…does that mean that rails will ignore any other configuration settings for config.assets?
For example,
config.assets.debug = true etc
I'm fairly sure this isn't doing anything, but can't be 100% sure.
It seems this setting was removed. Check out the following threads:
https://github.com/rails/rails/pull/44525
https://github.com/rails/sprockets-rails/issues/15
Basically, if you want to fully disable the Rails Asset Pipeline you should remove the sprockets-rails gem.
You are disabling asset pipeline in this line application.rb
rails.config.assets.enabled = false
So expectation now is all asset related configuration will not change anything. Same applied to
config.assets.debug = true

Long reloading time in Rails development

I have application, quite big on Rails 5.0.7.2 and Ruby 2.6. But for sometime local development is real pain. When I change one file and try to reload endpoint which serves only JSON response it took almost a minute to get response. I noticed that all controllers under app/controllers and subdirectories are getting loaded once again, like in the first request after booting the app. Serving responses without code changes is fast, only after changing something in code it took long time on first reload.
My config/environments/development.rb file looks like:
Webapp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Allow to see previews on development.
config.action_mailer.show_previews = true
config.action_mailer.preview_path = Rails.root.join('spec', 'mailers', 'previews')
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found).
config.i18n.fallbacks = true
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = false
# Generate digests for assets URLs.
config.assets.digest = true
# Fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Use an evented file watcher to asynchronously detect changes in source code,
# caching actions and page fragments
# routes, locales, etc. This feature depends on the listen gem.
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end
I have already tried EventedFileUpdateChecker, but it didn't change anything.
Most of the controllers I have under app/controllers/api directory in Api module.
I am developing on OSX.
It was not caused by the framework configuration itself. All my issues disappeared when I removed gems like better_errors, meta_request and caller_binding from my Gemfile.

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?
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.
I created the following gist (https://gist.github.com/metaskills/9028312) that does just this and found it is the only way that works for me.
# In config/initializers/sprockets.rb
require 'sprockets'
require 'sprockets/server'
Sprockets::Server.class_eval do
private
def headers_with_rails_env_check(*args)
headers_without_rails_env_check(*args).tap do |headers|
if Rails.env.development?
headers["Cache-Control"] = "no-cache"
headers.delete "Last-Modified"
headers.delete "ETag"
end
end
end
alias_method_chain :headers, :rails_env_check
end
The accepted answer is not doing it correctly and it degrades the performance in development by disabling cache totally.
Answering your original question, you want changes to referenced files to invalidate the asset cache even if not included directly.
The solution is by simply declaring such dependency such that sprockets knows that the cache should be invalidated:
# layout.html.erb
<% depend_on Rails.root.join('app').join('views').join('_partial.html.erb') %>
# replace the above with the correct path, could also be relative but didn't try

Rails, sprockets, google closure and advanced opts

I've added the closure-compiler gem to my Gemfile and set
config.assets.js_compressor = :closure
in the config/environments/production.rb file.
I believe this defaults to using the SIMPLE_OPTIMIZATIONS compilation level and I was wondering if there is a config variable I can set somewhere to specify the advanced level instead.
I tried digging through the sprockets code but haven't found a way to pass options to the js_compressor yet.
Check out this issue:
https://github.com/rails/rails/issues/2693
To put in simple terms, the given solution is:
# config.assets.js_compressor = :closure
require 'closure-compiler'
config.assets.js_compressor = Closure::Compiler.new(compilation_level: 'ADVANCED_OPTIMIZATIONS')

Why are changes to coffeescript files not being compiled when my Rails 3.2.0 app is in development mode?

Normally, any changes I make to .js.coffee files in my Rails 3.2.0 app in development mode take effect when I refresh the page. All of a sudden, this is not happening. If I do rake assets:precompile, then the changes are shown, but then if I do rake assets:clean they go back to not being shown. What is causing this?
Edit: Restarting the server makes the changes show. Why isn't this happening automatically as before?
Edit: Here is my development.rb
Myapp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.log_level = :warn
end
I'm quite new in Rails (well... I only learn programming for <3 months
I would guess:
You or some installation change your config in config/application.rb
Can you check in the file:
Is config.assets.initialize_on_precompile being false?
Change to true and try again.
Also check config.assets.compile. Is it true?
You might change the default value in Rails 3.2. So, I suggest you to think why the change(s) happens.

Resources