简体   繁体   English

为什么Heroku上的Rails应用程序通过all.css和本地通过单个文件来提供资产

[英]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.

相关问题 Heroku Rails 3 all.css / all.js动态生成? - Heroku Rails 3 all.css / all.js dynamic generation? 如何在Heroku上通过带有Rails的https通过Cloudfront资产提供服务? - How to serve cloudfront assets via https with rails on heroku? ActiveAdmin可以通过Rails资产管道为开发中的单个javascript文件提供服务吗? - Can ActiveAdmin serve individual javascript files in development via Rails asset pipeline? Ruby on Rails CSS无法通过资产管道加载 - Ruby on Rails CSS not loading via assets pipeline 为 Rails 应用程序在本地加载 CSS 但不在 Heroku 中加载 - CSS loading locally but not in Heroku for a rails app Heroku:Rails应用静态资产太大而无法上传应用,如何提供资产 - Heroku: Rails app static assets too large to upload app, how to serve assets Rails应用程序仅在Heroku上无法在本地运行 - Rails app does not work locally only on heroku Rails config.assets.precompile 设置来处理 app/assets 中的所有 CSS 和 JS 文件 - Rails config.assets.precompile setting to process all CSS and JS files in app/assets Heroku Rails 3.1应用程序-在本地编译资产与在段编译期间编译资产 - Heroku rails 3.1 app - compiling assets locally vs compiling assets during slug compilation 通过Heroku部署时,CSS在Safari上中断,但在本地运行良好吗? - CSS breaks on Safari when deployed via Heroku, but works perfectly locally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM