资产未与 Heroku 和 Cloudflare 一起使用

assets not being used with Heroku and Cloudflare

我有 ruby on rails 6 应用程序在 heroku 上 运行ning,我正在将其设置为 运行 在自定义域上。我做了以下

heroku domains:add www.example.com
heroku domains:add example.com
heroku domains # noted the herokudns.com names for www.example.com and example.com
# in cloudflare added cnames for www.example.com and example.com
# in cloudflare, checked the example.com cname is flattened
heroku config:set APPLICATION_HOST=www.example.com
heroku config:set ASSET_HOST=www.example.com

当我访问 www.example.com 时,我看到了原始 html,但没有样式。但是在日志中我看到

2020-08-22T05:42:36.262127+00:00 heroku[router]: at=info method=GET path="/packs/js/application-aaf9d9c806bf765590e6.js" host=www.example.com request_id=e833945b-54bc-4d4a-bbf7-d7c502444b15 fwd="2001:8003:2d82:b901:5821:c882:4a71:5ba0,108.162.250.97" dyno=web.1 connect=0ms service=6ms status=200 bytes=302 protocol=https
2020-08-22T05:42:36.261767+00:00 heroku[router]: at=info method=GET path="/assets/application-90a7667ebe1530b7281dc7743ca54268ab23286f578c9a356684ecd84321b0fd.css" host=www.example.com request_id=d2ffabfa-fe6e-4ef9-b6a4-d8bd771181dc fwd="2001:8003:2d82:b901:5821:c882:4a71:5ba0,108.162.250.69" dyno=web.1 connect=0ms service=8ms status=200 bytes=331 protocol=https

如果我查看 <head> 标签中页面的源代码,我有

<link rel="stylesheet" media="all" href="https://www.philomaths.org/assets/application-90a7667ebe1530b7281dc7743ca54268ab23286f578c9a356684ecd84321b0fd.css" />
    <script src="https://www.philomaths.org/packs/js/application-aaf9d9c806bf765590e6.js"></script>

因此,正在加载正确的资产文件。但是当我查看资产文件时,js和css都是空的。

我该如何解决这个问题?

我做了一个小改动并将它推送到 heroku 以确保它编译正确,我看到了这条消息

remote:        You set `config.action_dispatch.x_sendfile_header = 'X-Sendfile'` in production,
remote:        but you do not have `apache` installed on this app. This setting will cause any assets
remote:        being served by your application to be returned without a body.
remote:        
remote:        To fix this issue, please set:
remote:        
remote:        config.action_dispatch.x_sendfile_header = nil

我意识到我设置错误 the x_sendfile_header。我在 production.rb 和
中修复了它 然后推送到heroku,还是一样的问题

然后我使用了 heroku repo:resetheroku repo:purge_cache 命令,重置了 Cloudflare 缓存,并清除了我的浏览器缓存,并且......现在可以正常工作了!