问题描述
我正在使用Rails 5.2.3
I am using rails 5.2.3
我可以在本地下载pdf,但在 Heroku 上,会出现错误500.
Locally I can download the pdf but on Heroku it through an error 500.
错误为: ActionView :: Template :: Error(资产管道中不存在资产"pdf.css".):
这是我的heroku日志
Here are my heroku logs
2019-06-12T20:26:05.707039+00:00 app[web.1]: I, [2019-06-12T20:26:05.706962 #4] INFO -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] Rendered clients/orders/show.pdf.erb within layouts/clients/application.pdf.erb (38.4ms)
2019-06-12T20:26:05.709399+00:00 app[web.1]: I, [2019-06-12T20:26:05.709314 #4] INFO -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] Completed 500 Internal Server Error in 55ms (ActiveRecord: 16.2ms)
2019-06-12T20:26:05.710722+00:00 app[web.1]: F, [2019-06-12T20:26:05.710296 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0]
2019-06-12T20:26:05.710724+00:00 app[web.1]: F, [2019-06-12T20:26:05.710384 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] ActionView::Template::Error (The asset "pdf.css" is not present in the asset pipeline.):
2019-06-12T20:26:05.710726+00:00 app[web.1]: F, [2019-06-12T20:26:05.710579 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] 1: <html lang="fr">
2019-06-12T20:26:05.710728+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] 2: <head>
2019-06-12T20:26:05.710732+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] 3: <meta charset="utf-8" />
2019-06-12T20:26:05.710733+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] 4: <%= wicked_pdf_stylesheet_link_tag 'pdf' %>
2019-06-12T20:26:05.710734+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] 5: </head>
2019-06-12T20:26:05.710736+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] 6: <body>
2019-06-12T20:26:05.710737+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] 7: <div>
2019-06-12T20:26:05.710739+00:00 app[web.1]: F, [2019-06-12T20:26:05.710659 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0]
2019-06-12T20:26:05.710815+00:00 app[web.1]: F, [2019-06-12T20:26:05.710734 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] app/views/layouts/clients/application.pdf.erb:4:in `_app_views_layouts_clients_application_pdf_erb___1711932080367152756_47199839415040'
2019-06-12T20:26:05.710823+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] app/controllers/clients/orders_controller.rb:19:in `block (2 levels) in show'
2019-06-12T20:26:05.710825+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] app/controllers/clients/orders_controller.rb:16:in `show'
在我的views/layouts
中我有application.pdf.erb
<html lang="fr">
<head>
<meta charset="utf-8" />
<%= wicked_pdf_stylesheet_link_tag 'pdf' %>
</head>
<body>
<div>
<%= yield %>
</div>
</body>
</html>
pdf文件的存储方式如下vendor/assets/stylesheets/pdf.scss
,在供应商中,我也有一个自定义的引导文件bootstrap.min.css
The pdf file is stored like so vendor/assets/stylesheets/pdf.scss
, in vendor I also have a customized bootstrap file bootstrap.min.css
pdf.scss
@import "bootstrap.min";
.someclass {}
在config/initializers/assets.rb
中,我有以下一行:
Rails.application.config.assets.precompile += %w( pdf.scss, chartkick.js)
我也在使用webpacker,这也许是问题所在……
I am also using webpacker and that maybe here the problem...
这是我的 webpacker.yml
# Note: You must restart bin/webpack-dev-server for changes to take effect
default: &default
source_path: app/javascript
source_entry_path: packs
public_output_path: packs
cache_path: tmp/cache/webpacker
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
extensions:
- .jsx
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: /node_modules/
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Cache manifest.json for performance
cache_manifest: true
在公共/资产文件中(在本地),并且在heroku上没有pdf文件的痕迹....
In public/assets file (locally) and on heroku there is no trace of the pdf file....
我在这里想念什么?
我可以转发更多代码,只是让我知道...我真的希望你能帮助我.
I can forward more code, just let me know... I really hope you could help me.
这是我的package.json我移动了webpack-cli,但仍然不起作用:(我又部署了,我应该运行一个新命令使它正常工作吗?
Here is my package.json I moved webpack-cli but it still doesn't work :( I deployed again, Am I supposed to run a new command to make this working ?
{
"name": "kamelot",
"private": true,
"dependencies": {
"@rails/webpacker": "3.5",
"babel-preset-react": "^6.24.1",
"flatpickr": "^4.5.7",
"highcharts": "^7.1.1",
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-color": "^2.17.3",
"react-dom": "^16.8.6",
"react_ujs": "^2.5.0",
"webpack-cli": "^3.3.4"
},
"devDependencies": {
"webpack-dev-server": "2.11.2"
}
}
推荐答案
这是一个远景,但是有一个已知问题,其中在package.json
配置文件中将webpack-cli
标记为devDependency
.
This is a long shot, but there is a known issue with Webpacker v4 in which webpack-cli
is marked as a devDependency
in package.json
configuration file.
当部署任务运行webpacker:compile
并且没有webpack-cli
时,不会引发任何错误,因此部署完成,但是public/packs
文件夹中没有文件被编译.
When deploy task runs webpacker:compile
and there is no webpack-cli
, there is no error thrown, so the deploy finish but no file is compiled in public/packs
folder.
要解决此问题,您应该将webpack-cli
依赖项简单地从devDependencies
移到dependencies
.
To fix this, you should simple move the webpack-cli
dependency outside of devDependencies
to dependencies
.
更新
如果使用capistrano
部署项目,则应在deploy.rb
配置文件中的linked_dirs
中添加public/packs
和node_modules
(最后一个是可选的):
If you are using capistrano
to deploy your project, you should add public/packs
and node_modules
(the last is optional) to your linked_dirs
in deploy.rb
configuration file:
此外,如果您使用node_modules,则必须在预编译资产之前运行yarm:install
.为此,只需将此任务添加到您的deploy.rb
文件中:
Also if you use node_modules, you must run yarm:install
before precompile your assets. To do that, simply add this task in your deploy.rb
file:
before "deploy:assets:precompile", "deploy:yarn_install"
namespace :deploy do
desc "Run rake yarn install"
task :yarn_install do
on roles(:web) do
within release_path do
execute("cd #{release_path} && yarn install --silent --no-progress --no-audit --no-optional")
end
end
end
end
有关更多信息,请查看 webpacker文档用于capistrano部署.
For more details on this, check webpacker documentation for capistrano deployment.
更新2
这是另一个远景,但是有一个报告的问题,其中heroku
buildpacks的顺序可能会影响资产编译过程. node
buildpack必须首先出现,然后是ruby
buildpack.以下应解决此问题:
This is another long shot, but there is a reported issue in which the order of heroku
buildpacks can affect assets compilation process. node
buildpack must come first, and then ruby
buildpack. The following should fix the issue:
heroku buildpacks:clear
heroku buildpacks:set heroku/nodejs
heroku buildpacks:add heroku/ruby
同一问题中建议的另一个快速解决方案是在heroku
控制台中运行rake webpacker:compile
,但这不能解决部署资产编译问题.
Another quick fix suggested in the same issue is to run rake webpacker:compile
inside heroku
console, but this doesn't fix deploy assets compilation problem.
这篇关于使用webpacker部署到heroku后,资产管道中不存在资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!