为什么heroku上的rails应用程序通过all.css和本地通过单个文件提供资源 [英] Why does a rails app on heroku serve assets via all.css and locally via individual files

查看:104
本文介绍了为什么heroku上的rails应用程序通过all.css和本地通过单个文件提供资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我使用

我是一名Rails新手,我一直在试图弄清楚heroku上stylesheets_link_tag的情况。

  = stylesheet_link_tagstyle,:cache => true 

heroku使用all.css并且不会选取样式表,但如果使用

  = stylesheet_link_tagstyle,:cache => false 

它使用名称style.css来提供样式表。为什么?

解决方案

这是在您的样式表链接标记上调用:cache => true的结果。



:cache => true将提供所有提供的样式表,并将它们连接成一个名为all.css的文件。



只有在您的Heroku部署中看到这一点的原因是,它只有在Rails应用程序以生产模式运行时才会调用all.css级联。



例如,假设我有三个样式表,并将它们包含在我的标题中:

  = stylesheet_link_tagapplication,jquery-ui,style,:cache => true 

在开发过程中,它将包含application.css,jquery-ui.css和style。 css(以该顺序)。



在生产中,它将三个文件中的所有CSS(按提供的顺序)连接到一个名为all的文件中。 css,这将是唯一的CSS文件。



好处是在制作中减少HTTP请求,理想情况下为您包含的CSS减小文件大小,加快页面加载速度。



Edit 正如Casper在评论中指出的那样,Heroku有一个只读文件系统。
您可能需要查看 Heroku资产打包程序以了解Heroku特定的解决方案。


I'm a rails newbie, I've been trying to figure out what is going on with the stylesheets_link_tag on heroku.

If I use

= stylesheet_link_tag "style", :cache => true

heroku uses "all.css" and does not pick up the stylesheet, but if I use

= stylesheet_link_tag "style", :cache => false

it serves the stylesheet using its name "style.css". Why?

解决方案

This is the result of calling :cache => true on your stylesheet link tag.

:cache => true takes all of the stylesheets provided and concatenates them into one file called 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.

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).

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.

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.

Edit As Casper points out in the comments, Heroku has a read-only filesystem. You might want to look at Heroku Asset Packager for a Heroku-specific solution.

这篇关于为什么heroku上的rails应用程序通过all.css和本地通过单个文件提供资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆