Understanding Rails config.assets - when assets are not enabled - ruby-on-rails

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

Related

Bullet-proofing asset pipeline specs in Rails (5.2) - how force test failure if ANY model.css or model.coffee is missing from asset pipie

Is there a way, in Rspec, to force a failure if any of the "Rails better find this file or else" assets are missing?
In production, the app fails with the exception ActionView::Template::Error: The asset "foo.css" is not present in the asset pipeline
That's because the file is missing, because it would be blank, because it's for a rarely viewed backend model.
Sure, I could add a quick spec or two to "hit" the page. (In theory a simple "view" spec won't work, unless you also render the template, and the layout, which is extra complex when devise authentication is involved.)
But what I want is for Rspec is simple: to automatically fail with "model.css is missing from the asset pipelines" whether or not there is a spec in place to "hit" that asset.
Whenever possible we simply want the app to fail, hard, right in Test if it's going to do that anyway on Production... that kind of being the point of test.
None of the following worked when added to test.rb:
config.assets.compile = false / true
config.assets.digest = true / false
config.assets.check_precompiled_asset = false / true
config.assets.raise_runtime_errors = true / false
This seems like a useful safeguard to avoid situations where specs pass and production throws exceptions because Rails rquires some pointless and empty file that does not exist (and is not declared in the asset pipelines).

Disable the sprockets assets caching in rails 5+

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

duplicate Rails 4 asset fingerprint file on Heroku

I'm getting duplicate asset fingerprint javascript file on Heroku production.
This initially creates around 3-4 files then after a while (a day), it creates another set of those files again. Also every time I refresh those files get rotated in the source.
On production.rb:
config.assets.enabled = true
config.assets.digest = true
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.assets.initialize_on_precompile = true
config.assets.precompile += %w( '.woff', '.eot', '.svg', '.ttf', '*.css.scss', application_user.js, popcorn.js )
On application.rb:
config.assets.enabled = true
config.assets.digest = true
Surely this doesn't matter?
Structure
The Rails structure is such that it should allow you to use whatever fingerprinted file you need, and it will show (using the dynamic javascript include helpers)
If you're unable to read a particular file because it's not exactly the same as it was before is, in my opinion, a highlight of a poor system design
Files
I think I remember your issue from another day -- you can just use the helper method to call the files you need. It shouldn't cause any issues with the different names. It's all part of the asset pipeline
I'd recommend looking into how you're calling the files - if you're trying to call the hashed filename directly, you're going to have an issue

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 asset pipeline and javascript files - maintaining line breaks to aid debugging

I recently migrated from Jammit to the Rails Asset Pipeline. Other than a few teething issues, everything has been working well.
However, I recently started getting some script errors in production, and realised that it's near on impossible for me to debug them. I had previously configured Jammit to retain linebreaks, but otherwise remove all white space in the javascript files. This was to ensure that should I see a runtime error, I would be able to locate the offending line and hopefully figure out what the problem is. With the Rails Asset Pipeline, and the default :uglifier compressor, it appears all whitespace is removed including line breaks, and as such my script errors do not tell me where in the code the problem was.
Does anyone know anyway to configure the Rails Asset Pipeline to retain line breaks so that code can be debugged?
Matt
Set in you production.rb:
config.assets.compress = false
and running rake assets:precompile won't uglify your assets.
UPD:
So-called compression means (among other stuff): remove line breaks and comments.
But if you want to obfuscate your variables and save some readability then use:
# in production.rb
config.assets.compress = true
config.assets.js_compressor = Uglifier.new(:beautify => true) if defined? Uglifier
Here see for more options: https://github.com/lautis/uglifier.

Resources