Heroku는 Rails 4의 자산 파이프 라인에서 파일을 컴파일하지 않습니다.
Rails 4 및 Ruby 2.0의 자산 파이프 라인을 통해 모든 것이 로컬 머신에서 잘 작동합니다. 그러나 heroku에 배포 할 때 다음과 같이 표시됩니다.
-----> 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는 파일을 컴파일하는 것처럼 보이지만 오류없이 / tmp에 넣습니다. 내 질문은 다음과 같습니다.
- Heroku는 어떻게 자산 파일을 / tmp로 컴파일합니까?
- 내 마지막 해결책은 RAILS_ENV = production bundle exec rake assets : precompile 로컬로 실행하는 것이었지만, 이것은 manifest.yml이 아닌 public / assets에서 manifest-xxxxxx.json을 생성하여 heroku가 JSON manifest 파일을 감지하지 못하도록했습니다. json 파일에서 수동으로 yml을 생성하여 분류했고 heroku는 행복해졌습니다. heroku의 접근 방식이 구식입니까?
Rails 4는 플러그인을 지원하지 않기 때문에 Heroku의 자산 플러그인은 더 이상 작동하지 않습니다. 대신 Heroku의 자산 보석을 사용해야합니다. 이것을 Gemfile에 넣으십시오.
group :production do
gem 'rails_log_stdout', github: 'heroku/rails_log_stdout'
gem 'rails3_serve_static_assets', github: 'heroku/rails3_serve_static_assets'
end
Rails 4를 시작하려면 Heroku의 가이드 를 따르세요 .
업데이트 (2013 년 7 월 22 일) : 이제 Heroku는 자산을 사전 컴파일하기 위해 다른 gem을 제공합니다.
group :production do
gem 'rails_12factor'
end
프로덕션에서 정적 자산을 제공하려면 Rails를 구성해야합니다 : config / environments / production.rb
SampleApp :: Application.configure do . . . config.serve_static_assets = true . . . 종료
최신 정보:
Rails 4에서는 더 이상 사용되지 않으며 다음과 같이 변경되었습니다.
config.serve_static_files = true
이후 레일 4 manifest- (지문) .json으로 manifest.yml 교체 , 당신은 고정 자산 제공을 가능하게 할 것이다.
에서이 에게 Heroku에 레일 4.x를 시작하기 :
gem 'rails_12factor', group: :production
그때
bundle install
그리고 마지막으로,
git push heroku
나를 위해 문제를 해결했습니다. 도움이 되었기를 바랍니다!
나는 정확히 똑같은 문제에 부딪친 다.
heroku가 새 매니페스트 형식을 지원하지 않을 때까지 내 environment / production.rb 파일에서 config.serve_static_assets = true를 설정했습니다.
따라서 heroku 지원이 추가 될 때까지 일시적인 해결책입니다.
Heroku에 대한 가이드 나 StackOverFlow에 대한 제안이 저에게 도움이되지 않은 몇 시간 동안 인터넷 검색을 한 후 마침내이 단서를 제공하는 이 블로그 게시물 을 보게되었습니다 .
heroku labs:enable user-env-compile --app=YOUR_APP
이것이 없으면 자산 파이프 라인은 항상 전체 앱을 초기화하고 데이터베이스에 연결하려고 시도합니다 (레일 4가 더 이상이 작업을 수행한다는 것을 읽은 모든 내용에도 불구하고). 이렇게하면 Heroku 구성이 Rails에 노출되어 성공적으로 부팅하고 assets : precompile과 같은 레이크 작업을 실행할 수 있습니다.
이 보석을 사용해야했습니다.
gem 'rails_12factor', group: :production #need this for rails 4 assets on heroku
그리고 /config/environments/production.rb에서 다음을 설정해야했습니다.
config.assets.compile = true
내 이해는 무엇보다도 rails_12_factor
보석이 설정 된다는 config.serve_static_assets = true
것입니다.
제 경우에는 자산이 위의 지침에 따라 컴파일되었지만 부트 스트랩 글 리피 콘 'fontawesome-webfont'를 선택하지 않았기 때문에 조사에 많은 시간을 낭비한 후 마침내 저에게 효과적이었습니다.
보석 파일
gem 'rails_12factor', group: :production
번들 설치
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
그리고 마침내 나는 rake assets:precompile RAILS_ENV=production
그것을 헤 로쿠에 밀어 넣고 작동했습니다.
이것은 Heroku Ruby 빌드 팩의 문제 였지만 오늘 업데이트가 배포되었습니다 (2013-05-21). 그것을 시도하고 알려주십시오.
질문에 답하려면 :
# 1) 이것은 스프로킷 출력입니다. 사물이 컴파일 된 /tmp
다음 이동됩니다 ( 스프로킷 참조 ). 내가 아는 한 이것은 항상 이런 방식으로 수행되었지만 Rails에서 Sprockets 버전이 업데이트 될 때까지 새로운 디버그 유형 출력을 얻지 못했습니다.
#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.
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
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_paths
differs:
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.
Apart from ensuring you have the 'rails_12factor' gem installed the only thing you need to do is this.
# config/application.rb
config.assets.paths << Rails.root.join('vendor', 'assets')
It seems that although Rails knows exactly what it wants, Heroku needs reminding to include the assets folder as part of the assets paths.
Use Image Extensions
I had this same issue, but for a different reason.
Instead of
<%= asset_path 'facebook-link' %>
Use:
<%= asset_path 'facebook-link.png' %>
While the first one worked locally, when I pushed to Heroku my images were breaking and I had no clue why. Using the full file extension fixed the problem :)
Add this gem gem 'rails_serve_static_assets'
https://github.com/heroku/rails_serve_static_assets
If you are using controller specific assets as in:
<%= javascript_include_tag params[:controller] %> or <%= javascript_include_tag params[:controller] %>
Then in production you will need to explicitly precompile those (in development rails compiles files on the fly).
See official Rails guide here: http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets
To precompile as explained in the guides (here: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets) you will need to add the following to the config/application.rb
# 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 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.
This may not answer the original question's root cause, But I was having a similar symptom with a different root cause.
Pre-compilation of a JPEG files changes the file extension to JPG, meaning that asset_path("my_image.jpeg")
and asset_path("my_image")
didn't work. Remove the "e" from JPEG and voila, it works.
Others have described the same problem here https://blazarblogs.wordpress.com/2016/04/06/rails-force-to-precompile-jpeg-to-jpg/
Is this a bug? Or desired behaviour? And also weird that it only doesn't work in my Heroku-hosted production environment. Maybe they have some sort of configuration.
'Nice programing' 카테고리의 다른 글
인라인 변수로 여러 줄 Python 문자열을 어떻게 생성합니까? (0) | 2020.10.13 |
---|---|
포함 옵션을 사용하여 특정 유형의 파일에만 rsync 복사 (0) | 2020.10.13 |
Postgres에서 열거 형 값을 삭제하는 방법은 무엇입니까? (0) | 2020.10.13 |
라벨로 선택하는 Pandas는 때때로 Series를 반환하고 때로는 DataFrame을 반환합니다. (0) | 2020.10.12 |
팻 모델 / 씬 컨트롤러 대 서비스 레이어 (0) | 2020.10.12 |