In a Rails 3 application, I would like to be able to use unminified javascript and css files in my development environment for debugging and such, and minified versions in production.
I can think of a few hacky solutions, but ideally I'd like javascript_include_tag and friends to automatically select the right asset file.
Ideas?
Add your js normally using
javascript_include_tag
In your development.rb set:
config.assets.compress = false
and in production.rb use:
config.assets.compress = true
Related
Rails: 4.2.7.1, Ruby: 2.3.1
We used to use non-hashed assets for serving our assets on our multi-site application by hardcoding the path to the asset in the individual site's headers. We are now pre-compiling hashed assets with Sprockets on each deploy in production with these settings:
production.rb
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = true
config.assets.prefix = "/assets"
config.assets.digest = true
config.assets.initialize_on_precompile = true
We end up with hashed assets, for example, in /public/assets/skins/site1/skin-70ee38249235f8521a243a8f955ed08c.css.
We have a skin file for each path that we use to load our styles per site. We do generate a site application.css that remains unused. Each skin file contains variables for each site and then imports the site's manifest file containing the sass stylesheets.
What we tried to do was create a way for our CMS to look for the hashed asset:
stylesheet_link_tag("/assets/skins/site1/skin.css", media: "all")
The issue is that the site is still looking for the non-hashed skin file. If we have the plain skin.css, it will work.
Same goes for Javascript:
javascript_include_tag "/assets/admin.js"
It will look for the non-hashed asset, even though the hashed asset is present. I tried to remove the "/assets" part from the link tags and it will fail.
Is there a setting that we are missing to have Rails look for the proper hashed asset? Any advice is appreciated.
I am referencing assets in my rails app directly, for ex. background-image: url('/assets/bg.png').
But I just realized this wouldn't work in production since digested assets are served (/assets/bg-dddasd434r4tfdfs...sada.png) in production.
Is my only choice to use helper methods (for ex, image-url) throughout the application or is there a more simpler solution to this?
You can try asset_path('app.js', :digest => false) or disable digest in the prodution env:
# config/environments/prodution.rb
config.assets.digest = false
Rails pipeline
In config/application.rb change the value of
config.assets.digest to false
This is an ancient question, but since I happened upon it with Google...
In Rails 5 there is the asset_path helper which even in the .scss files can return the proper path to a file.
In development it seemed to work well, I used to load the files like this:
$(".div").load("/assets/svg_file.svg");
But now that I have pushed the project to Heroku, it gave me a 404 error inside my console. It also happened with some images I included, but it was fixed by changing the default html image tag to this:
<%= image_tag("logo.png") %>
Since I can't include any ruby code on the client side, how do I do it?
If any user faces the same issue, go to production.rb file (config => environments => production.rb) and change config.assets.compile = false to config.assets.compile = true. This should fix it.
I'm writing a Rails4 app that uses a custom cache manifest file which needs to include references to all the required Javascript and CSS files. Due to the nature of the application, the Rack Offline gem can't be used.
The stylesheet_link_tag and javascript_include_tag calls produce the correct list of files (as generated by the asset pipeline) but embed them in HTML tags.
Is there a way to get the paths to all the compiled javascript and stylesheet files in the controller?
eg.
/assets/custom.css?body=1
/assets/incidents.css?body=1
/assets/users.css?body=1
/assets/application.css?body=
/assets/jquery.js?body=1
/assets/bootstrap/affix.js?body=1
...
That one was fun! Had to go into the Sprockets source to figure it out.
asset_list = Rails.application.assets.each_logical_path(*Rails.application.config.assets).to_a
You can then go in a grep through the asset list, something like:
asset_list.grep(/\.(js|css)/)
EDIT:
If you want the hex digests, you could do something like:
environment = Rails.application.assets
asset_list = environment.each_logical_path(*Rails.application.config.assets).to_a
asset_list.map! { |asset| environment.find_asset(asset).digest_path rescue nil }.compact
Based on #kdeisz research, this code worked in the controller for the manifest file:
#assets = Rails.application.assets.each_logical_path(*Rails.application.config.assets).to_a
#assets = #assets.map{ |p| view_context.compute_asset_path(p) }
render 'manifest.text', content_type: 'text/cache-manifest'
The compute_asset_path function is needed to get the actual asset path.
Note: I haven't yet tested this in production mode. It works in development mode if you set config.assets.debug = false
All my javascripts stopped working in production when I compiled them into application.js. I fixed this issue by turning asset debug on in production like so:
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = false
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
config.assets.debug = true
my question is if there are any problems with leaving asset debug on in production mode besides people being able to see my js files. Is there a setting I'm missing that would allow me to compress the JS and it still would work? I'd like to be able to compress them but the javascript just isn't loading when I try it.
Sometimes compressed js just doesn't work, could be because of the order it was done, or something else.