为什么heroku上的rails应用程序通过all.css和本地通过单个文件提供资源

时间:2022-01-27 00:23:26

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

我是一个铁杆新手,我一直试图弄清楚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". Why?

它使用名称“style.css”为样式表提供服务。为什么?

3 个解决方案

#1


17  

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. You might want to look at Heroku Asset Packager for a Heroku-specific solution.

编辑正如Casper在评论中指出的那样,Heroku有一个只读文件系统。您可能希望查看Heroku Asset Packager以获取Heroku特定的解决方案。

#2


1  

Tested this and it did not work for me (adding config.serve_static_assets = true to production.rb)

测试了这个,它对我不起作用(将config.serve_static_assets = true添加到production.rb)

#3


0  

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. Finding a way to serve my css and js files concatenated is on my to-do list.

我对Cloudflare背后的表现稍微不那么担心。找到一种方法来连接我的css和js文件是在我的待办事项列表中。

#1


17  

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. You might want to look at Heroku Asset Packager for a Heroku-specific solution.

编辑正如Casper在评论中指出的那样,Heroku有一个只读文件系统。您可能希望查看Heroku Asset Packager以获取Heroku特定的解决方案。

#2


1  

Tested this and it did not work for me (adding config.serve_static_assets = true to production.rb)

测试了这个,它对我不起作用(将config.serve_static_assets = true添加到production.rb)

#3


0  

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. Finding a way to serve my css and js files concatenated is on my to-do list.

我对Cloudflare背后的表现稍微不那么担心。找到一种方法来连接我的css和js文件是在我的待办事项列表中。