When I push my app to heroku certain assets are not loading and I cannot figure out why. One of the things that I noticed is that the stylesheets that are not loading are being loaded with the path:
stylesheets/stylesheet.css
instead of
assets/stylesheet.css
In my css folder I have an .scss file in there, and that seems to be loading just fine but the others are not loading.
This is a rails 4 app.
I've played around with my settings and even installed the following gems:
"rails_12factor"
"rails_serve_static_assets"
"rails_stdout_logging"
My application.rb is as follows:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env)
module myApp
class Application < Rails::Application
end
end`
Production.rb is a bit longer, but for the settings related to assets I have:
config.serve_static_assets = false
config.assets.compile = false
config.assets.digest = true
config.assets.version = 1.0
Does anybody have any suggestions on where to begin in order to diagnose this issue?
Thanks!
Have you tried the steps on the Heroku documentation about precompiling assets? https://devcenter.heroku.com/articles/rails-asset-pipeline
Related
Rails Admin works locally for me, but when I deploy to Heroku, it fails with:
ActionView::Template::Error (The asset "rails_admin/rails_admin.css" is not present in the asset pipeline
I am on Rails 5.2.6 and Rails Admin 2.0.2
I added initializers/assets.rb with this line:
Rails.application.config.assets.precompile += ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']
but those files do not exist in assets/javascripts and assets/stylessheets.
This app was originally built as an API only app, but I've added all the missing middleware to make it work as a normal Rails app.
This is my application.rb:
require_relative 'boot'
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CheckwizApi
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = false
# use uuid's as primary_key
config.active_record.primary_key = :uuid
config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
end
config.i18n.default_locale = :en
config.active_job.queue_adapter = :sidekiq
# Added because RailsAdmin needs them
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Flash
config.middleware.use Rack::MethodOverride
config.middleware.use ActionDispatch::Session::CookieStore
end
end
Note that I have set config.api_only = false and added back the required middleware, but to no avail. If I set it to true, same error.
If I remove the assets file I added above, same error.
The assets/javascripts and assets/stylesheets folders are empty. Is that the problem?
I have looked at several other closed issues related to this and most of them advise adding the above middleware, or clearing tmp/cache and restarting. Neither worked for me sadly.
A number of things had to be done to fix this.
Firstly, I updated my config/initializers/assets.rb file to this:
Rails.application.config.assets.enabled = true
# Prevent initializing the application before assets are precompiled (required for heroku)
Rails.application.config.assets.initialize_on_precompile = false
# Add Rails Admin assets (required)
Rails.application.config.assets.precompile += ['rails_admin/rails_admin.css', 'rails_admin/rails_admin.js']
Then, by examining the Rails logs on Heroku:
heroku logs -t --remote production
I spotted this line:
Detected manifest file, assuming assets were compiled locally
So inside public/assets I deleted the manifest file (it is created if you compile assets locally, which perhaps I had done once to try to solve this issue, and forgot it was there).
This then caused the following error to appear in the heroku log:
Sprockets::ArgumentError: link_directory argument must be a directory
So I then added a hidden empty file called .keep to both the javascripts and stylesheets folders inside app/assets
Now Heroku runs the asset precompile task as part of the deploy (ie. whilst building the slug) and the rails_admin js and css files are created.
More info here and here which helped figure this out.
I have a ruby on Rails 4.2 app and am facing a "hair-tearing" issue for long 2 days about my asset pipeline. My prod is hosted on Heroku and I directly mention this here as I think it might be relevant I have them gem 'rails_12factor', group: :production
I read and tried the suggestions of the (too) numerous SO questions about Rails assets not compiling but none worked as I'll describe further down.
The issue observed which led me to this SO question is that my my javascript application.js file was NOT minified in production.
How do I know? the white spaces are still here, the comments have not been removed, well the applciation.js, contrary to my application.css, is just a concatenatation of all js files but NO compressing/minifying has been done.
Most questions on SO deal with issues where neither images, css or js is minified/precompilied but my situation is peculiar to the extent that images, css are precompiled/minified, but only js is a problem and is not minified.
Is there a problem with my js? (see below for some experiments I tried to find out the reason of the bug) Seems not
My set up below will show you how I deal with assets (to the extent of my beginner understanding) : I use guard to constantly monitor any change and precompile stuff and put the resulting/generated application-tr56d7.css (fingerprinted) and application-45dsugdsy67.js ((fingerprinted) inside public/assets and then when I deploy on git, it pushes all changes , including the precompiled/minified files and then when I push to Heroku, my production asset settings say to Rails to deploy my already precompiled assets. I'm a beginner and struggled with understanding all the numerous dev/prod environment assets settings but I think that is what is defined in the code you'll find further down.
I know all this process it's working because everytime i change a file when I can find a new application-tr56d7.css and a new application-45dsugdsy67.js(examples of course) (along with a new css.gz and.js.z which must be the binary stuff)
Every time I change a js file for example, guard make his job and I can read something like:
I, [2018-02-09T09:53:41.140165 #130534] INFO -- : Writing /home/mathieu/rails_projects/my_app/public/assets/application-af0ab4a348e4f5545c844cfac02a0670.js
The new generated application.css and application.js files can then be found in public/assets folder: for example
/public/assets/application-1021e7d2ea120fe40c67ec288f1c6525.js
/public/assets/application-1021e7d2ea120fe40c67ec288f1c6525.js.gz (binary: just a list of numbers...)
/public/assets/application-753e1d0958f76ae233a460ad47606790.css
/public/assets/application-753e1d0958f76ae233a460ad47606790.css.gz (binary: just a list of numbers...)
So when I observed that in production the css was application-753e1d0958f76ae233a460ad47606790.css minified but not the application-1021e7d2ea120fe40c67ec288f1c6525.js js, I went to see on
public/assets and here too I noticed the same thing:
the css files generated by guard such as /public/assets/application-753e1d0958f76ae233a460ad47606790.css css are minified
but the js files genrated by gyard are NOT minified
So I think that, but I'm not sure, it's not a Heorku specific problem, it's just that even before pushing it to Heroku, my js file hosted on public/assets should be but is not minified.
What i tried to debug this (spoiling the suspenese:all failed):
tried to say explicitly in assets.rb to compile application.js not work => Result: js still not minified locally and not on production website on heroku
# It is recommended to make explicit list of assets in config/application.rb
config.assets.precompile = ['application.js']
tried cleaning all old stuff by rake :assets clobber => Result: js still not minified locally and not on production website on heroku
tried cleaning old stuff by changing version
assets.rb: changed => Result: js still not minified locally and not on production website on heroku
config.assets.version = '1.0'
into
config.assets.version = '1.1'
tried to change all the various dev and prod asset settings => Result: js still not minified locally and not on production website on heroku
config.serve_static_assets = true and tried false
tried also so many different settings for both files but none worked.
tried to compile in local and in prod => Result: js still not minified locally and not on production website on heroku
rake assets:precompile RAILS_ENV=production --trace
Also tried local:
rake assets:precompile
2 weirdest attempt:
after all my attempts at making this work by modifying the asset pipeline settings which all failed, I thought maybe there 's a tricky javascript error somewhere breaking silently the minification/compressing made by guard (which also be silent in terms of page load as no error appear on chrome dev tools when i load my pages but who knows...read on some SO questions some strange effect of comments on the asset pipeline)', so
I decided to comment ALL my js files inside assets/javascripts/! nothing left: and even removed the js that ends up in the pipeline but injected by a gem (so not visible in my folder app/assets/javascripts) such as jquery gem: and create 2 very basic js files
that would be the only remaining files...
well still : => Result: js still not minified locally and not on production website on heroku
Did again the same test as above but here went even further: I emptied (deleted all the content) of all the js files inside my assets/javascripts and removed form application.js all the gems to only leave require directory , and then created 2 very simple js files to check if it was minified now....
and still same result: js still not minified locally and not on production website on heroku
A test that kind of worked
doing rake assets:clobber then the push (git add, git commit, git push, git push heroku paster) DOES compile the js BUT unfortunately it creates other issues: it sends to the production an OLD version of the js (I know because i put a alert message inside the js and it's not the latest one!). What does it reveal about the bug that rake assets:clobber kind of debug it?
EDIT
I made it work but with quite a demanding process:
leveraging some people saying there is no compilation if you don't change css or js (see https://stackoverflow.com/a/7988689/1467802), I tweaked the previous process: (change sth in the js file, rake assets:clobber, git add, git commit, git push, git heroku master) and it works: it cpmpiles and sends the latest js file!
Isn't there any way not to have to remeber eveyr time to change sth inside the js to ensure compilation ?
I'm out of ideas. Maybe my settings are just wrong and as a beginner, I'm missing something obvious.
The weirdest poart is: my cs is minified! why not the js????
My codebase
/config/environments/development.rb
MyApp::Application.configure do
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
config.serve_static_files = false
end
/config/environments/production.rb
MyApp::Application.configure do
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_files = false
# Compress JavaScripts and CSS
# config.assets.compress = true removed when switch from Rails 3.2 to Rails 4
config.assets.js_compressor = :uglifier
config.assets.js_compressor = Uglifier.new(
# Remove all console.* functions
:compress => { :drop_console => true }
) if defined? Uglifier
config.assets.css_compressor = :sass
# 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
config.force_ssl = true
end
/config/initializers/assets.rb
Rails.application.configure do
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# related to deployment pb with active admin
config.assets.precompile += %w[admin/active_admin.css admin/active_admin.js]
# for ckeditor: github.com/galetahub/ckeditor
config.assets.precompile += %w( ckeditor/* )
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
end
/config/application.rb
require File.expand_path('../boot', __FILE__)
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
module MyApp
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/lib)
# Enable the asset pipeline
config.assets.enabled = true
end
end
/Guardfile
# More info at https://github.com/guard/guard#readme
require 'active_support' # edded this due to a bug where guard did not load github.com/rails/rails/issues/14664
require 'active_support/core_ext'
require 'active_support/inflector'
# Make sure this guard is ABOVE any other guards using assets such as jasmine-headless-webkit
# It is recommended to make explicit list of assets in `config/application.rb`
# config.assets.precompile = ['application.js', 'application.css', 'all-ie.css']
# update dec 2014- added :runner => :cli because of a know bug on guard rail assets
# if bug is solved i can remove the part :runner=> cli
guard 'rails-assets', :run_on => [:start, :change], :runner => :cli do
watch(%r{^app/assets/.+$})
watch('config/application.rb')
end
assets/javascripts/application.js
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require jquery.cookie
//= require cloudinary-jquery.min
//= require twitter/bootstrap
//= require paloma
//= require html5shiv-printshiv
//= require storageService
//= require turbolinks
//= require_directory .
Terminal process when deploying after, for example chagning some js files and waiting guard has notified me it has finished its precompilaiton job:
git add --all
git commit -a -m "fix issue with asset pipeline"
git push
git push heroku master
I know what I am going to say is not conventional, but your aproach hasn't been so explicit, you should try adding the explicit compress call you have made false in development
/config/environments/production.rb
MyApp::Application.configure do
# Compress assets please
config.assets.compress = true
# ... other stuff
end
After that clean your assets and regenerate them with
$ bundle exec rake assets:clobber
$ RAILS_ENV=production bundle exec rake assets:precompile
If I understand this correctly, you can replicate this issue locally, so the Heroku factor is irrelevant.
However, it sounds like you are compiling the assets locally, committing them to repository, then pushing to Heroku. First of all, I'd avoid this and lean on letting Heroku do the static asset compilation during deploy.
Regardless, if I understand correctly that this is what you are doing, I think it may because when you run rake assets:precompile, you may be compiling them in dev mode, which will use your config/development.rb configuration, which has config.assets.compress = false. I'm not sure why some of your files are compressed while others aren't, other than it may simply be related to how recently you've modified the source files.
In any case, try running:
$ rake assets:clean
$ rake assets:precompile RAILS_ENV=production
I suspect you will have issues booting the app in production mode locally (e.g. if you don't have database credentials configured appropriately or something), which is another reason why I would not precompile assets locally prior to deploy. However, this may prove or disprove if the active environment is a factor in your issues.
In fact, you could try running this on the Heroku dyno instead, which will be setup for production already:
(local)$ heroku run bash
(heroku)$ rake assets:clean assets:precompile RAILS_ENV=production
(heroku)$ less public/assets/application-*.js # see if this is compressed
Let me know what you discover here and I can revise my answer if that doesn't change the equation for you
I followed this tutorial on how to compress assets in Heroku.
http://www.cheynewallace.com/serving-compressed-assets-with-heroku-rack-zippy/
Here is my Application.rb file
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module Blog
class Application < Rails::Application
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
config.exceptions_app = self.routes
config.cache_store = :memory_store
Rails.application.config.middleware.swap(ActionDispatch::Static,
Rack::Zippy::AssetServer, Rails.public_path)
end
end
My Gem File
gem 'yui-compressor', '~> 0.12.0'
gem 'rack-zippy'
I ran heroku run rake middleware and got eh following output
use Rack::Sendfile
use Rack::Zippy:AssetServer
Now when I go to Developer Tools and the Network Tab, I see that the size is nearly identical to the content for every asset. Which means the assets were not compressed.
The only similar question I could find was this one
rack-zippy and option -d in production env doesn't serve static assets
And the answer didn't work.
Should I use another Compressor, because it looks like Rack Zippy does not work.
I had a similar problem. Things started to work when I set config.assets.compile to false on config/environments/production.rb.
In addition is necessary pre-compile the assets before deploy.
Upgraded from Rails 3 to 4, and now the image assets aren't showing in production. It appears that the javascript and css files are working ok.
I've tried the steps at https://github.com/capistrano/capistrano/wiki/Upgrading-to-Rails-4 but it doesn't help.
I've tried deleting the json files and yml files in my shared/assets folder, but it still doesn't show the images once the deploy finishes.
If I try to view an image on the site in a new tab, I get the "The page you were looking for doesn't exist." error.
I've tried this:
Bundler.require(:default, Rails.env)
and this:
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
in the config/application.rb file. Neither work.
Here's what I have in my config/environments/production.rb file:
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
The Capfile:
load 'deploy'
load 'deploy/assets'
load 'config/deploy'
What am I doing wrong? How can I get the images to show up in production?
Rails 4 no longer creates assets without a digest. So if you are referencing assets (such as images) directly (e.g. /assets/whatever.jpg) without a digest in the URL, you will likely get a 404.
non-stupid-digest-assets gem will fix this by creating non-digest assets: https://github.com/alexspeller/non-stupid-digest-assets/tree/master
This is all my bad...I haven't been using the asset pipeline helpers, and now it's biting me. When I switch to image_tag("whatever.jpg"), it working...
So, of course I figure this out after an hour of messing with my main SASS file and wondering why I couldn't see the changes -- but it turns out that my local host isn't paying any attention to what I'm putting in my SASS files anymore.
This comes just after having switched everything over from SCSS to SASS, and I'm 100% sure the file is just getting ignored. For kicks/proof, I deleted all the styling in my application.erb.sass file (the only one with any styling any more -- I consolidated to get to the bottom of this), then saved, then restarted the server, and it's looking as styled as ever.
This seems to be an asset pipeline issue, and since I don't really know what to do with my config files, I'll paste the relevant-seeming stuff here:
config/environments/development.rb has these lines:
# Do not compress assets
config.assets.compress = false
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
# Expands the lines which load the assets
config.assets.debug = true
config/application.rb
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
# Enable the asset pipeline
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/fonts"
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
Also, I've got "gem 'sass-rails'" in my gem file, not in any group.
Think that's all/most that's relevant. Any idea how to fix this?
run : rake assets:clean && assets:precompile
The problem seems to have been the extension on my css file (which had some embedded ruby):
erb.sass and erb.css.sass didn't work, but css.erb.sass did.
This was inexplicably stopping the file from being read, so that when I ran assets:clean/precompile, there was no css left. Before I ran that, I guess it was running my pre-compiled CSS file from before. Weird.
Apparently, these things must be compiled from the right to the left (as probably everybody but me knew), so CSS should be the type it's compiled into.
Sorry for the confusion.