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.
I am receiving this error when trying to compile assets on Heroku
Custom asset_path helper is not implemented
Extend your environment context with a custom method.
environment.context_class.class_eval do
def asset_path(path, options = {})
end
end
I have tried pretty much everything I could find through google. Heroku seems to be getting caught up when precompiling the assets active_admin.css, which contains 2 #import statements. If I comment those out, the build proceeds, but without the active admin stylesheets working in production.
If anyone has any thoughts, please advise. Thank you.
I fixed the issue by removing all config in application.rb relating to asset precompiling, and added a catch all compile script. I guess somehow it was missing a file, which caused the error:
# Precompile all assets
# application.rb
config.assets.precompile << Proc.new do |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
end
When I run my application with nginx and passenger, not even single css or js loading..it show me following error.
cache: [GET /assets/application-403103e41ab40b92f00b841ac9afb23a.js] miss
Started GET "/assets/application-403103e41ab40b92f00b841ac9afb23a.js" for localhost at 2013-10-07 10:48:13 +0530
ActionController::RoutingError (No route matches [GET] "/assets/application-403103e41ab40b92f00b841ac9afb23a.js"):
cache: [GET /assets/jquery.blockUI-49c048033e3dfb8cfab8eff7edbdc553.js] miss
Started GET "/assets/jquery.blockUI-49c048033e3dfb8cfab8eff7edbdc553.js" for localhost at 2013-10-07 10:48:18 +0530
ActionController::RoutingError (No route matches [GET] "/assets/jquery.blockUI-49c048033e3dfb8cfab8eff7edbdc553.js"):
In my config/environments/production.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
In config/application.rb
config.assets.precompile << Proc.new do |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
end
I have compiled every file separately as it is requirement for my app, my application.css and application.js file dont have any manifest included. This files are blank.
Please help...
Use Case: Push my Rails 4.rc1 app to Heroku, go through asset pre-compilation, and then use asset_sync gem to put them on S3. I have the asset_host set for S3 in the config.
After this but during the slug compilation, I'd like to dump a fingerprinted asset URL to Redis so that other Heroku apps can see it (they're sharing the same Redis db) and use the same assets files. Essentially:
//s3.amazonaws.com/my_bucket/assets/desktop-(fingerprint).css
Obviously, the fingerprint changes between deploys and the other apps needs the updated URL.
Here's my first attempt:
module AssetShare
class << self
def capture_urls
action_controller = ActionController::Base.new
REDIS.set('desktop_css_url',action_controller.view_context.stylesheet_url('desktop'))
REDIS.set('mobile_css_url',action_controller.view_context.stylesheet_url('mobile'))
end
end
end
desc 'Capture asset pipeline stylesheet and javascript URLS to Redis'
task 'assets:capture_urls' => :environment do
AssetShare.capture_urls
end
# stolen from asset_sync
if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
AssetShare.capture_urls
end
else
Rake::Task["assets:precompile"].enhance do
AssetShare.capture_urls
end
end
This dumped out:
//s3.amazonaws.com/my_bucket/stylesheets/desktop.css
Then I found this resource, but Sprockets has changed in Rails 4: http://blog.noizeramp.com/2011/10/14/asset-urls-and-paths-in-rake-tasks/
Here's my second attempt:
desc 'Capture asset pipeline stylesheet and javascript URLS to Redis'
task 'assets:capture_urls' => :environment do
MyApp::Application.configure do
config.assets.debug = false
config.assets.digest = true
end
include ActionView::Helpers::AssetTagHelper
desktop_url = stylesheet_url('desktop', only_path: false)
REDIS.set('desktop_css_url', desktop_url)
puts "Saved desktop url to Redis for Store Rails app: #{desktop_url}"
mobile_url = stylesheet_url('mobile', only_path: false)
REDIS.set('mobile_css_url', mobile_url)
puts "Saved mobile url to Redis for Store Rails app: #{mobile_url}"
end
# stolen from asset_sync
if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
Rake::Task["assets:capture_urls"].invoke
end
else
Rake::Task["assets:precompile"].enhance do
Rake::Task["assets:capture_urls"].invoke
end
end
This only dumped out:
/stylesheets/desktop.css
Obviously the first attempt it closer. I just can't get it to dump the fingerprinted url. If I run the rake task via the heroku toolkit, the correct, fingerprinted URL is printed. Thoughts?
Ultimately, this is the solution I had to go with:
desc 'Capture asset pipeline stylesheet and javascript URLS to Redis'
task 'assets:urls:capture' => :environment do
# unfortunately, this way doesn't work when deploying
#controller = MyApp::Application::ApplicationController.new
#desktop_url = controller.view_context.stylesheet_url('desktop')
#mobile_url = controller.view_context.stylesheet_url('mobile')
desktop_url = MyApp::Application.config.action_controller.asset_host
desktop_url += "/" + AssetSync.storage.get_remote_files.select{|d| d.include?('desktop') && d.include?('.css') }.first
REDIS.set('desktop_css_url', desktop_url)
puts "Saved desktop url to Redis for Store Rails app: #{desktop_url}"
mobile_url = MyApp::Application.config.action_controller.asset_host
mobile_url += "/" + AssetSync.storage.get_remote_files.select{|d| d.include?('mobile') && d.include?('.css') }.first
REDIS.set('mobile_css_url', mobile_url)
puts "Saved mobile url to Redis for Store Rails app: #{mobile_url}"
end
desc 'Clear asset pipeline stylesheet and javascript URLS from Redis'
task 'assets:urls:clear' => :environment do
REDIS.del('desktop_css_url')
puts "Cleared desktop url from Redis"
REDIS.del('mobile_css_url')
puts "Cleared mobile url from Redis"
end
# stolen from asset_sync
if Rake::Task.task_defined?("assets:precompile:nondigest")
Rake::Task["assets:precompile:nondigest"].enhance do
Rake::Task["assets:urls:capture"].invoke
#Rake::Task["assets:urls:clear"].invoke
end
else
Rake::Task["assets:precompile"].enhance do
Rake::Task["assets:urls:capture"].invoke
#Rake::Task["assets:urls:clear"].invoke
end
end
I could never get the proper fingerprinted URL to be dumped during deployment to Heroku. It says it's running as the production environment, though. This gives me a URL in Redis like:
//s3.amazonaws.com/my_bucket/assets/desktop-6acd36e3d6c8d2b6e0fe58b83690687f.css
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
}