I have deployed my app to heroku, but some assets are not loading, e.g.:
GET https://myapp.herokuapp.com/javascripts/s3_direct_upload.js - 404
GET https://myapp.herokuapp.com/stylesheets/s3_direct_upload_progress_bars.css - 404
the problem is in precompile, I have to add every file manually to assets.rb
but I don't really want to do it, because there are many of them
in my assets.rb I tried:
Rails.application.config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
}
and:
Rails.application.config.assets.precompile = false
but it's not working
I've added to application.rb
config.serve_static_files = true
also, //=require from my precompilled assets are not included into header
everything works good in dev environment on my laptop
what do I need to change to make it work in production?
Update
I use helper, to include some js and css files in views:
def javascript(*files)
content_for(:foot) { javascript_include_tag(*files) }
end
and in my view I have:
<% stylesheet 's3_direct_upload_progress_bars' %>
<% javascript 's3_direct_upload', 'init.script.js' %>
assets from gem s3_direct_upload are not loading
init.script.js is located in assets/javascripts, so it is loading as it should be
another problem, in my application.js I have:
//= require jquery
//= require jquery.slicknav.min.js
$(function ()
{
$('#menu').slicknav();
});
after assets:precompile it looks okay, but in console I have error:
$(...).slicknav is not a function
so it was compiled wrong? everything works good in development environment
Update 2
Ignore the second problem, I found second require for jquery, it caused this error
but I still can't include assets from gem without precompile,
can I somehow disable this behavior? I just want include some assets for specific actions without headache
By default Rails 4 will not serve your assets. To enable this functionality you need to go into config/application.rb and add this line:
config.serve_static_assets = true
Alternatively you can achieve the same result by including the rails_12factor gem in your Gemfile:
gem 'rails_12factor', group: :production
This gem will configure your application to serve static assets so that you do not need to do this manually in a config file.
Hope this will work for you.
Recently I changed my apps environment to Production. After that I had error message in log files.
ActionView::Template::Error (jquery.fancybox.pack.js isn't precompiled):
So I tried execute rake assets:precompile, but nothing happened. Error stayed the same.
In Stack Overflow I found sugestion to add this code to production.rb
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
}
Then I rake assets:precompile again. Now I got some action like:
including asset: /home3/ecotec11/rails_apps/technorent/app/assets/javascripts/active_admin.js
including asset: /home3/ecotec11/rails_apps/technorent/app/assets/javascripts/ga.js
including asset: /home3/ecotec11/rails_apps/technorent/app/assets/javascripts/jquery-1.9.1.min.js
....
excluding asset: /home3/ecotec11/rails_apps/technorent/vendor/bundle/ruby/1.9.3/gems/tinymce-rails-3.5.9/vendor/assets/javascripts/tinymce/themes/advanced/skins/highcontrast/ui.css
excluding asset: /home3/ecotec11/rails_apps/technorent/vendor/bundle/ruby/1.9.3/gems/tinymce-rails-3.5.9/vendor/assets/javascripts/tinymce/themes/advanced/skins/o2k7/content.css
Now I am browsing my web app, but all style and design is gone..
What could cause this problem ?
Thanks!
If you use bootstrap, try to clean temp by rake tmp:clear. Needs more information about your gems, and log.
Oh. I did bad research in google.
Found that my log files displayed routing error:
ActionController::RoutingError (No route matches [GET] "/assets/4_600_350-764568941999af034523c215c97b086f.jpg"):
Found that I need set in production.rb file
config.serve_static_assets = true
I have been playing around with this deployment issues for a while now. Basically it seems like that the fonts files are passed into the public/assets/ folder after the assets:precompile (I have deleted the whole assets folder and let Rails to recompile again). No matter what I have tried, the css still comes out with the path without fingerprint. Can anyone help to identify what's wrong with my code? I know I can do some monkey works to get around it by just copying the fonts to the public/assets but I think it is not a good practice in a long run.
Here's the setup:
Resulting Client-side CSS:- No fingerprint at all...
#font-face {
font-family:cabinmedium;
src:url(cabin-medium/cabin-medium-webfont.eot?#iefix) format(embedded-opentype),url(cabin-medium/cabin-medium-webfont.woff) format(woff),url(cabin-medium/cabin-medium-webfont.ttf) format(truetype),url(cabin-medium/cabin-medium-webfont.svg#cabinmedium) format(svg);
font-weight:400;
font-style:normal;
}
fonts.css.scss:
#font-face {
font-family: 'cabinmedium';
src: font-url('cabin-medium/cabin-medium-webfont.eot');
src: font-url('cabin-medium/cabin-medium-webfont.eot?#iefix') format('embedded-opentype'),
font-url('cabin-medium/cabin-medium-webfont.woff') format('woff'),
font-url('cabin-medium/cabin-medium-webfont.ttf') format('truetype'),
font-url('cabin-medium/cabin-medium-webfont.svg#cabinmedium') format('svg');
font-weight: normal;
font-style: normal;
}
And of coz in the production.rb, I have added the fonts path in the assets path:
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
In case it is of someone's interests, here is the assets.rb in the config/initializers/ folder:
Rails.application.config.assets.precompile << Proc.new do |path|
if path =~ /\.(css|js|coffee|scss)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
end
Finally I have managed to track down the problem!
I need to explicitly declare using the assets pipeline in production.rb:
config.assets.enable = true
Then, in the assets.rb in initializers, I include the fonts but do not include the .coffee and .scss
Rails.application.config.assets.precompile << Proc.new do |path|
if path =~ /\.(css|js|eot|svg|ttf|woff)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
end
The problem I had before is caused by two reason:
When I include .scss in assets precompile, there is some problem with the compilation of the scss. From Rails' documentation, it mentioned that scss and coffee do not need to be included because the compiled files (.css and .js) will be included. That may cause some problem with the font-url. The reason behind is unknown so if someone can explain that would be cool!
Need to include the fonts extensions: eot|svg|ttf|woff so that they are included in the pipeline.
I hope this would help someone.
It has happened to me & my colleagues that we forgot to add a .js or a .css to the list of precompiled files. I want to change the production.rb file to scan the javascripts/stylesheets folder, and add the dynamically to the config.assets.precompile variable. Code looks something like this:
def get_assets(folder,select_extensions,delete_from,remove_extensions)
files = Dir["#{folder}/**/*"].select {|e| select_extensions.include?(File.extname(e))}
basenames = files.map {|e| e.sub(/.*#{delete_from}\//,"") }
basenames.each do |bn|
remove_extensions.each do |ext|
bn.sub!(ext,"")
end
end
basenames
end
And in production.rb, I will have something like this:
config.assets.precompile = get_assets("#{Rails.root}/app/assets/javascripts",[".js",".coffee"],"javascripts",[".coffee"]) + get_assets("#{Rails.root}/app/assets/stylesheets",[".css",".scss"],"stylesheets",[".scss"])
Do you see any drawbacks/pitfalls to my idea?
I wish to precompile all the CSS and JS files in my project's app/assets folder. I do NOT want to precompile everything in vendor/assets or lib/assets, only the dependencies of my files as needed.
I tried the following wildcard setting, but it incorrectly precompiles everything. This results in lots of extra work and even causes a compilation failure when using bootstrap-sass.
config.assets.precompile += ['*.js', '*.css']
What is my best bet to only process my files in app/assets? Thanks!
config.assets.precompile = ['*.js', '*.css']
That will compile any JavaScript or CSS in your asset path, regardless of directory depth. Found via this answer.
A slight tweak to techpeace's answer:
config.assets.precompile = ['*.js', '*.css', '**/*.js', '**/*.css']
I would've added a comment to his answer but I don't have enough reputation yet. Give me an upvote and I'll be there!
NOTE: this will also precompile all the CSS/JavaScript included via rubygems.
This task is made more difficult by the fact that sprockets works with logical paths that do not include where the underlying, uncompiled resourced is located.
Suppose my project has the JS file "/app/assets/javascripts/foo/bar.js.coffee".
The sprockets compiler will first determine the output file extension, in this case ".js", and then the evaluate whether or not to compile the logical path "foo/bar.js". The uncompiled resource could be in "app/assets/javascripts", "vendor/assets/javascripts", "lib/assets/javascripts" or a gem, so there is no way to include/exclude a particular file based on the logical path alone.
To determine where the underlying resource is located, I believe it is necessary to ask the sprockets environment (available via the object Rails.application.assets) to resolve the real path of the resource given the logical path.
Here is the solution that I am using. I am fairly new to Ruby so this is not the most elegant code:
# In production.rb
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
if full_path.starts_with? app_assets_path
puts "including asset: " + full_path
true
else
puts "excluding asset: " + full_path
false
end
else
false
end
}
With sprockets > 3.0, this will not work in production because Rails.application.assets will be nil (assuming default: config.assets.compile = false).
To workaround you replace the full_path assignment with:
#assets ||= Rails.application.assets || Sprockets::Railtie.build_environment(Rails.application)
full_path = #assets.resolve(path)
See also: https://github.com/rails/sprockets-rails/issues/237
I found this in the rails code:
#assets.precompile = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) },
/(?:\/|\\|\A)application\.(css|js)$/ ]
Which is backed up with the rails guide:
The default matcher for compiling files includes application.js,
application.css and all non-JS/CSS files
This default is not reset if you use +=, so you need to override it with a = instead of +=. Note that, apparently, you can pass a Proc or a regex to precompile as well as an extension. I believe, if you want to preompile only files in the top level directory, you will have to create a regex like:
config.assets.precompile = [ /\A[^\/\\]+\.(ccs|js)$/i ]
I'm revisiting this post at the year 2017.
Our product is still heavily using RoR, we've been continuously modifying our precompile configurations by appending Rails.application.config.assets.precompile as we're adding new modules. Recently I was trying to optimize this by adding a regex pattern, I found that the following glob pattern works:
Rails.application.config.assets.precompile += %w(**/bundle/*.js)
However I still need to exclude certain modules, so I tried hard to use regex instead of glob.
Until I looked into sprockets source code: https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L108 , I found that they're already using regex:
app.config.assets.precompile +=
[LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
So I change my code into:
Rails.application.config.assets.precompile +=
[/^((?!my_excluded_module)\w)*\/bundle\/\w*\.js$/]
Which works well.
This will get all .css .scss and .js including all files in subdirectories.
js_prefix = 'app/assets/javascripts/'
style_prefix = 'app/assets/stylesheets/'
javascripts = Dir["#{js_prefix}**/*.js"].map { |x| x.gsub(js_prefix, '') }
css = Dir["#{style_prefix}**/*.css"].map { |x| x.gsub(style_prefix, '') }
scss = Dir["#{style_prefix}**/*.scss"].map { |x| x.gsub(style_prefix, '') }
Rails.application.config.assets.precompile = (javascripts + css + scss)
I wanted all assets from both /app and /vendor to be compiled, except partials (which name starts from underscore _). So here is my version of an entry in application.rb:
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
app_assets_path = Rails.root.join('app', 'assets').to_path
vendor_assets_path = Rails.root.join('vendor', 'assets').to_path
if ((full_path.starts_with? app_assets_path) || (full_path.starts_with? vendor_assets_path)) && (!path.starts_with? '_')
puts "\t" + full_path.slice(Rails.root.to_path.size..-1)
true
else
false
end
else
false
end
}
Additionally it outputs list of files being compiled for debugging purposes...
This snippet includes all js/css files, excluding gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass"). It also has a strategy for including assets from Gems which aren't included in every page.
# These assets are from Gems which aren't included in every page.
# So they must be included here
# instead of in the application.js and css manifests.
config.assets.precompile += %w(a-gem.css a-gem.js b-gem.js)
# This block includes all js/css files, excluding gems,
# under: app/assets, vendor/assets, lib/assets
# unless they are partial files (e.g. "_file.sass")
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
aps = %w( /app/assets /vendor/assets /lib/assets )
if aps.any? {|ap| full_path.starts_with?("#{Rails.root}#{ap}")} &&
!path.starts_with?('_')
puts "\tIncluding: " + full_path.slice(Rails.root.to_path.size..-1)
true
else
puts "\tExcluding: " + full_path
false
end
else
false
end
}
Although, you probably wouldn't want to do this since you'd likely be precompiling gem assets twice (basically anything that is already \=require'd in your application.js or css). This snippet includes all js/css files, including gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass")
# This block includes all js/css files, including gems,
# under: app/assets, vendor/assets, lib/assets
# and excluding partial files (e.g. "_file.sass")
config.assets.precompile << Proc.new { |path|
if path =~ /\.(css|js)\z/
full_path = Rails.application.assets.resolve(path).to_path
asset_paths = %w( app/assets vendor/assets lib/assets)
if (asset_paths.any? {|ap| full_path.include? ap}) &&
!path.starts_with?('_')
puts "\tIncluding: " + full_path
true
else
puts "\tExcluding: " + full_path
false
end
else
false
end
}