在 Heroku 上使用 Compass:/tmp 用于远程和本地样式表

发布于 2024-10-25 15:01:38 字数 1568 浏览 2 评论 0原文

我目前正在 Heroku 中使用 Compass,使用 Heroku 知识库推荐的配置。 Heroku 有一个只读文件系统,因此编译后的样式表需要存储在 /tmp 中。这在 Heroku 上远程运行得很好;然而,在本地,Rails 希望在 /public/stylesheets 中找到样式表(当通过 = stylesheet_link_tag 'screen.css', :media => 'screen,projection' 调用时)。

为了解决这个问题,我使用 ln -s tmp/stylesheets/screen.css public/stylesheets/screen.css 在 /public/stylesheets 中创建了符号链接,这似乎有效。

有没有一种方法可以在不使用符号链接的情况下解决这个问题,也许可以通过更改 Rails 中的某些配置来解决?我已经四处寻找但没有取得多大成功。

这是我的 config/initializers/compass.rb:

require 'compass'
require 'compass/app_integration/rails'
Compass::AppIntegration::Rails.initialize!

# Required for Heroku:
require 'fileutils'
FileUtils.mkdir_p(Rails.root.join("tmp", "stylesheets"))

Compass::AppIntegration::Rails.initialize!

Rails.configuration.middleware.delete('Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Sass::Plugin::Rack')

Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
    :urls => ['/stylesheets'],
    :root => "#{Rails.root}/tmp")

这是我的 config/compass.rb:

project_type = :rails
project_path = Compass::AppIntegration::Rails.root

# Set this to the root of your project when deployed:
http_path = "/"

# Necessary for Heroku (original commented out:
css_dir   = 'tmp/stylesheets'
#css_dir = "public/stylesheets/compiled"

sass_dir  = 'app/views/stylesheets'

environment = Compass::AppIntegration::Rails.env

任何帮助将不胜感激。

I'm currently using Compass with Heroku using this configuration recommended on the Heroku knowledge base. Heroku has a read-only file system, and so the compiled stylesheets need to be stored in /tmp. This works fine remotely on Heroku; locally, however, Rails expects to find stylesheets in /public/stylesheets (when called through = stylesheet_link_tag 'screen.css', :media => 'screen, projection').

In order to solve the problem, I have created symbolic links in /public/stylesheets using ln -s tmp/stylesheets/screen.css public/stylesheets/screen.css and that seems to work.

Is there a way to solve this problem without using symbolic links, perhaps by changing some configuration in Rails? I've poked around without much success.

Here is my config/initializers/compass.rb:

require 'compass'
require 'compass/app_integration/rails'
Compass::AppIntegration::Rails.initialize!

# Required for Heroku:
require 'fileutils'
FileUtils.mkdir_p(Rails.root.join("tmp", "stylesheets"))

Compass::AppIntegration::Rails.initialize!

Rails.configuration.middleware.delete('Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Sass::Plugin::Rack')

Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
    :urls => ['/stylesheets'],
    :root => "#{Rails.root}/tmp")

And here is my config/compass.rb:

project_type = :rails
project_path = Compass::AppIntegration::Rails.root

# Set this to the root of your project when deployed:
http_path = "/"

# Necessary for Heroku (original commented out:
css_dir   = 'tmp/stylesheets'
#css_dir = "public/stylesheets/compiled"

sass_dir  = 'app/views/stylesheets'

environment = Compass::AppIntegration::Rails.env

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

[旋木] 2024-11-01 15:01:38

实际上,我正准备使用我们的 Rails 应用程序设置 Compass,该应用程序托管在 Heroku 上,所以为给我一个完成此工作的借口而欢呼。 :)

答案很简单:

修改 'config/compass.rb':

project_type = :rails
project_path = Compass::AppIntegration::Rails.root

http_path = "/"

environment = Compass::AppIntegration::Rails.env
if environment == 'production'
  css_dir = "tmp/stylesheets"
  sass_dir = "app/views/stylesheets"
else
  css_dir = "public/stylesheets"
  sass_dir = "app/stylesheets"
end

然后修改 'config/initializers/compass.rb':

require 'compass'
require 'compass/app_integration/rails'
Compass::AppIntegration::Rails.initialize!

require 'fileutils'
FileUtils.mkdir_p(Rails.root.join("tmp", "stylesheets"))

environment = Compass::AppIntegration::Rails.env
if environment == 'production'
  Compass::AppIntegration::Rails.initialize!

  Rails.configuration.middleware.delete('Sass::Plugin::Rack')
  Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Sass::Plugin::Rack')

  Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
      :urls => ['/stylesheets'],
      :root => "#{Rails.root}/tmp")
end

... 瞧,你很好。

I was actually just about to set up Compass with our Rails application, which is hosted on Heroku, so cheers for giving me an excuse to work through this. :)

The answer is simple:

Modify 'config/compass.rb':

project_type = :rails
project_path = Compass::AppIntegration::Rails.root

http_path = "/"

environment = Compass::AppIntegration::Rails.env
if environment == 'production'
  css_dir = "tmp/stylesheets"
  sass_dir = "app/views/stylesheets"
else
  css_dir = "public/stylesheets"
  sass_dir = "app/stylesheets"
end

Then modify 'config/initializers/compass.rb':

require 'compass'
require 'compass/app_integration/rails'
Compass::AppIntegration::Rails.initialize!

require 'fileutils'
FileUtils.mkdir_p(Rails.root.join("tmp", "stylesheets"))

environment = Compass::AppIntegration::Rails.env
if environment == 'production'
  Compass::AppIntegration::Rails.initialize!

  Rails.configuration.middleware.delete('Sass::Plugin::Rack')
  Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Sass::Plugin::Rack')

  Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
      :urls => ['/stylesheets'],
      :root => "#{Rails.root}/tmp")
end

... and voila, you're good.

夜清冷一曲。 2024-11-01 15:01:38

好吧,我自己就是 Heroku 和指南针的忠实粉丝,所以我已经看过

Heroku 的文档很多次了,虽然提供了正确的信息,但在这种情况下提供了糟糕的建议。

使用指南针时,99.999% 的情况下最好的做法是在生产模式下将其关闭。

这意味着您在开发计算机上编译样式表,然后将它们添加到 git 存储库,然后再推送到 heroku。

如果您允许指南针在服务器上进行编译,您将遭受相当大的性能损失。

所以这就是我所做的:

您的应用程序的基础上应该有一个 config.ru 文件。打开它并添加以下内容:

require 'sass/plugin/rack'
use Sass::Plugin::Rack
Sass::Plugin.options[:never_update] = true

然后,您可以从初始化程序中删除相当多的代码(尤其是卸载 Sass::Plugin::Rack 的部分)。此外,您还需要从 config 文件夹中的 compass.rb 中删除 if 语句。

想一想,为什么您希望 Sass 在服务器上编译样式表?它只会消耗处理能力。希望这有帮助,

编辑::
PS - 我应该补充一点,您现在需要从命令行运行 compass watch 以便让样式表在您的开发环境中进行编译

ok, I'm a big heroku and compass fan myself so i've been through this many times

Heroku's docs, whilst giving correct information, provide poor advice in this instance.

When using compass, the best thing to do, 99.999% of the time is turn it off in production mode.

This means that you compile your stylesheets on your development machine and then add them to your git repo before pushing to heroku.

You will suffer a reasonably sizeable performance hit if you allow compass to compile on the server.

So here's what I do:

You should have a config.ru file at the base of your app. Open it and add the following:

require 'sass/plugin/rack'
use Sass::Plugin::Rack
Sass::Plugin.options[:never_update] = true

You can then remove quite a lot of the code from your initializer (especially the part where you unload Sass::Plugin::Rack). Additionally you will want to remove the if statement from compass.rb in config folder

Think about it, why would you want Sass to compile a stylesheet on the server? It just eats up processing power. Hope this helps,

EDIT::
PS - I should add that you will need to run compass watch from the command line now in order to get your stylesheets to compile in your dev environment

唐婉 2024-11-01 15:01:38

推荐的 Heroku 配置也可以在本地运行。

  1. 删除了第二个“Compass::AppIntegration::Rails.initialize!”从 config/initializers/compass.rb 中,您只需要它一次。
  2. 确保您的 scss 文件位于“app/views/stylesheets”中

在本地和生产服务器上,样式表将被编译为 tmp/stylesheets,并且对 /stylesheets 的请求将解析为 tmp/stylesheest。无需两个单独的配置。

The recommended Heroku configuration will also work locally.

  1. Removed the second 'Compass::AppIntegration::Rails.initialize!' from config/initializers/compass.rb, you only need it once.
  2. Ensure your scss files are in 'app/views/stylesheets'

On both local and production servers the stylesheets will be compiled to tmp/stylesheets, and a request to /stylesheets will resolve to tmp/stylesheest. No need for two separate configurations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文