[英]Why does a rails app on heroku serve assets via all.css and locally via individual files
I'm a rails newbie, I've been trying to figure out what is going on with the stylesheets_link_tag on heroku. 我是Rails的新手,我一直在尝试弄清heroku上的stylesheets_link_tag是怎么回事。
If I use 如果我用
= stylesheet_link_tag "style", :cache => true
heroku uses "all.css" and does not pick up the stylesheet, but if I use heroku使用“ all.css”,并且不选择样式表,但是如果我使用
= stylesheet_link_tag "style", :cache => false
it serves the stylesheet using its name "style.css". 它使用名称“ style.css”提供样式表。 Why?
为什么?
This is the result of calling :cache => true on your stylesheet link tag. 这是在样式表链接标签上调用:cache => true的结果。
:cache => true takes all of the stylesheets provided and concatenates them into one file called all.css. :cache => true接受所有提供的样式表,并将它们连接到一个名为all.css的文件中。
The reason you're only seeing this on your Heroku deployment is because it calls the concatenated all.css only when the Rails application is running in production mode. 之所以只能在Heroku部署上看到此原因,是因为仅当Rails应用程序在生产模式下运行时,它才调用串联的all.css。
So for example let's say I have three stylesheets and I include them in my header: 例如,假设我有三个样式表,并将它们包含在标题中:
= stylesheet_link_tag "application", "jquery-ui", "style", :cache => true
When in development, this will include application.css, jquery-ui.css, and style.css (in that order). 在开发过程中,它将包括application.css,jquery-ui.css和style.css(按此顺序)。
In production, it will concatenate all of the CSS from the three files (in the order provided) into one single file called "all.css", which will be the only CSS file included. 在生产中,它将把三个文件(按提供的顺序)中的所有CSS连接到一个名为“ all.css”的文件中,这将是其中唯一的CSS文件。
The benefit is making fewer HTTP requests in production and ideally a smaller file size for your included CSS, which should hopefully speed up page load. 这样做的好处是可以减少生产中的HTTP请求,理想情况下,您所包含的CSS的文件大小应更小,这有望加快页面加载速度。
Edit As Casper points out in the comments, Heroku has a read-only filesystem. 编辑正如Casper在评论中指出的那样,Heroku有一个只读文件系统。 You might want to look at Heroku Asset Packager for a Heroku-specific solution.
您可能需要查看Heroku Asset Packager ,以获得针对Heroku的解决方案。
测试了它,它对我不起作用(将config.serve_static_assets = true添加到production.rb)
Setting :cache => true causes my requests to fail outright. 设置:cache => true会导致我的请求彻底失败。
My solution for the short term is to add the following to my config/environments/prodcution.rb 我短期的解决方案是将以下内容添加到我的config / environments / prodcution.rb中
config.serve_static_assets = true
I'm slightly less worried about the performance being behind Cloudflare. 我不太担心Cloudflare的性能。 Finding a way to serve my css and js files concatenated is on my to-do list.
在我的待办事项列表中找到一种方法来服务我串联的CSS和js文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.