Heroku does NOT compile files under assets pipelin

2019-01-04 07:52发布

Everything goes well in local machine with assets pipeline in Rails 4 and Ruby 2.0. But when deploying to heroku, it is shown that:

-----> Preparing app for Rails asset pipeline
   Running: rake assets:precompile
   I, [2013-03-12T03:28:29.908234 #912]  INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/rails-2ee5a98f26fbf8c6c461127da73c47eb.png
   I, [2013-03-12T03:28:29.914096 #912]  INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/trash-3c3c2861eca3747315d712bcfc182902.png
   I, [2013-03-12T03:28:33.963234 #912]  INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/application-bf2525bd32aa2a7068dbcfaa591b3874.js
   I, [2013-03-12T03:28:40.362850 #912]  INFO -- : Writing /tmp/build_1n6yi8lwna3sj/public/assets/application-13374a65f29a3b4cea6f8da2816ce7ff.css
   Asset precompilation completed (14.36s)

Heroku seems to compile files but put it in /tmp without any errors. My questions are:

  1. How come Heroku compile assets files to /tmp?
  2. My last solution was to run RAILS_ENV=production bundle exec rake assets:precompile locally, but this generated a manifest-xxxxxx.json in public/assets, rather than manifest.yml, so that heroku doesn't detect the JSON manifest file. I sorted it out by manually created a yml from the json file and heroku became happy. Has heroku's approach been outdated?

16条回答
劫难
2楼-- · 2019-01-04 08:08

Add this gem gem 'rails_serve_static_assets'

https://github.com/heroku/rails_serve_static_assets

查看更多
看我几分像从前
3楼-- · 2019-01-04 08:10

I added this to the top of one of my css.scss files in the assets/stylesheets/ folder.

@import "font-awesome";

then ran..

rake assets:clean

and...

rake assets:precompile RAILS_ENV=production
查看更多
\"骚年 ilove
4楼-- · 2019-01-04 08:10

In Rails 4.2.4 your production.rb has the line:

config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

That means, gem 'rails_12factor', group: :production doesn`t need to change it to true, as it can be set through the heroku environment variables. You also will get a warning if you remove the rails_12factor gem.

If you have problems with assets, login to the heroku console heroku run rails console and find out the asset path for a file puts helper.asset_path("application.js") .

One strange behaviour I noticed between development and production, when the file ending is not provided:

With a image /assets/images/image_01.jpg the following output from asset_pathsdiffers:

Development:

development > puts helper.asset_path('profile_01') 
=> /assets/profile_01-bbd16aac5ef1d295411af44c103fcc631ab90ee94957414d4c01c3aed1055714.jpg

development > puts helper.asset_path('profile_01.jpg') 
=> /assets/profile_01-bbd16aac5ef1d295411af44c103fcc631ab90ee94957414d4c01c3aed1055714.jpg

Production:

development > puts helper.asset_path('profile_01') 
=> /profile_01

development > puts helper.asset_path('profile_01.jpg') 
=> /assets/profile_01-bbd16aac5ef1d295411af44c103fcc631ab90ee94957414d4c01c3aed1055714.jpg

You do not have to run RAILS_ENV=production rake assets:precompile, heroku does this for you during deploy. Also you do not have to precompile the assets in development and push them to heroku.

查看更多
时光不老,我们不散
5楼-- · 2019-01-04 08:10

I figure I'll add this as an answer since this question is linked from the Heroku Support page if you search for "assets".

This is mostly for people who are updating their app to Rails 4, but after going through this - and many other SO posts - what finally got me was changing the following in production.rb:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

To:

config.action_dispatch.x_sendfile_header = nil

I hadn't caught this when I upgraded and as usual this took me forever to figure out. Hopefully it helps someone else! Shout out to PatrickEm who asked/answered the same in his question.

查看更多
唯我独甜
6楼-- · 2019-01-04 08:12

In my case, assets compiled following the instructions above but it wasn't picking the bootstrap glyphicons 'fontawesome-webfont' so this worked for me finally after wasting so many hours researching.

Gem file

gem 'rails_12factor', group: :production

bundle install

config/application.rb

config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif,
                                  "fontawesome-webfont.ttf",
                                 "fontawesome-webfont.eot",
                                 "fontawesome-webfont.svg",
                                 "fontawesome-webfont.woff")



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

environment/production.rb

config.serve_static_assets = true

Then finally, I ran rake assets:precompile RAILS_ENV=production and pushed it to heroku and that worked.

查看更多
疯言疯语
7楼-- · 2019-01-04 08:12

This was an issue with the Heroku Ruby Buildpack, but an update was deployed today (2013-05-21). Please try it out and let us know.

To answer your questions:

#1) This is sprockets output; things are compiled to /tmp and then moved (See here in Sprockets). To my knowledge this has always been done this way, but it wasn't until Sprockets version was updated in Rails that we got this new debug-type output.

#2) Previously assets:precompile genereated a manifest.json file, but now in Rails 4 the manifest file has a fingerprint in it, which wasn't detected previously. This was fixed with #74.

查看更多
登录 后发表回答