部署到子URI时,Rails 3.1中的预编译资产损坏 [英] Broken precompiled assets in Rails 3.1 when deploying to a sub-URI

查看:88
本文介绍了部署到子URI时,Rails 3.1中的预编译资产损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新Rails 3应用程序以使用Rails 3.1,作为其中一部分,我正在使用新的资产管道.到目前为止,除了我无法解决的一个相当烦人的问题之外,我已经将所有工作都进行了处理.

I'm in the process of updating a Rails 3 app to use Rails 3.1 and as part of that, am making use of the new asset pipeline. So far, I've got everything working apart from one rather annoying problem I can't solve.

该应用程序及其所有资产在开发中都可以正常工作,但在生产中,它使用Passenger(http://the-host/sub-uri/)部署到了子URI.问题在于,资产在部署过程中被预编译,而我的一个CSS文件(很好,它是一个.css.scss文件)正在使用sass-rails gem中的image-url助手.由于在预编译过程中,路径被硬编码到预编译的CSS文件中,因此sub-uri没有考虑:

The application and all its assets works fine in development, but in production it is deployed to a sub-URI using Passenger (http://the-host/sub-uri/). The problem with this is that the assets are pre-compiled during deployment and one of my CSS (well, it's a .css.scss file) files is making use of the image-url helper from the sass-rails gem. Since during the pre-compilation process, the paths are hard-coded into the precompiled CSS file, the sub-uri is not taken account of:

在我的.css.scss文件中:

body { background-image: image-url("bg.png"); }

已编译的application-<md5-hash-here>.css文件中的结果:

The result in the compiled application-<md5-hash-here>.css file:

body { background-image: url(/assets/bg.png); }

要使其正常工作应该是什么:

What it should be to make it work correctly:

body { background-image: url(/sub-uri/assets/bg.png); }

这种情况是否要求太多?如果是这样,我将不得不切换回旧的非资产流水线方式,只从public提供我的图像和CSS.但是,似乎有些事情应该被考虑和解决……?我错过了解决方案吗?

Is this scenario just asking too much? If so, I'll have to switch back to the old non-asset-pipelined way and just serve my images and CSS from public. However it seems like something which should have been thought about and solved...? Am I missing the solution?

我应该注意,使用 erb解决方案会产生与预期相同的结果.

Edit 1: I should note that using the erb solution instead yields the same result, as one would expect.

回应Benoit Garret的评论

否,问题与config.assets.prefix无关.我尝试将其设置为(/sub-uri/assets,而不是默认的/assets),但事实证明这是错误的做法-似乎此设置已经与Rails应用程序的根目录有关,而不是与服务器有关.删除该错误(并因此恢复为默认值)已解决了所有引起问题的怪异问题(并且很多资产都以/sub-uri/sub-uri/assets结尾-这非常奇怪).唯一的问题是image-url帮助器和朋友在预编译时不会选择子URI.不用说,这是合乎逻辑的,因为当对其进行预编译时,它可能无法知道当它在Passenger下运行时,是否将以这种方式进行配置.我的问题是如何告知这一点,从而最终获得预编译结果中的正确路径.如果确实可以做到的话.

No, the problem isn't related to the config.assets.prefix. I tried setting that (to /sub-uri/assets rather than the default of /assets) but it turned out that was the wrong thing to do - it seems like this setting is already in relation to the root of the Rails app, not the server. Removing that (and thus returning to the default) has fixed all the weird issues that caused (and there were many, all the assets ended up in /sub-uri/sub-uri/assets - it was all very strange). The only problem is that the image-url helper and friends do not pick up the sub-URI when they are pre-compiled. Needless to say, this is logical since when it is pre-compiled, it couldn't possibly know that when it's running under Passenger, it'll be configured in this way. My question is how to inform it of this and thus end up with the correct paths in the precompiled result. If indeed it can be done.

我当前的解决方法是像这样在CSS中引用iamge:url(../images/bg.png)并将其放置在非流水线public/images位置.几乎不理想,因为它无法从指纹识别和管道提供的所有内容中受益.

My current workaround is to reference the iamge in the CSS like this: url(../images/bg.png) and place it in the non-pipelined public/images location. Hardly ideal since it doesn't benefit from the fingerprinting and everything which the pipeline provides.

推荐答案

最后,我制定了一些解决方法/解决方案.

Finally I've worked out a couple of workarounds/solutions.

1)来自 https://github.com/rails/sass-rails/issues/17 似乎可以在sass-rails中修复.我已经按照上面链接中建议的补丁进行了猴子补丁helpers.rb的工作.我只需在deploy.rb的资产预编译行中设置所需的环境变量即可.

1) From https://github.com/rails/sass-rails/issues/17 it looks like this could get fixed in sass-rails. I've monkey-patched helpers.rb myself along the lines of the proposed patch in the link above. I simply set the required environment variable in the asset precompile line in deploy.rb.

我将所有猴子修补程序都放在一个文件config/initializers/gem_patches.rb中.在此文件中,我将此方法修补为:

I do all my monkey patching in a single file config/initializers/gem_patches.rb. In this file I patched this method as:

module Sass
  module Rails
    module Helpers
      protected
      def public_path(asset, kind)
        path = options[:custom][:resolver].public_path(asset, kind.pluralize)
        path = ENV['PRODUCTION_URI'] + path if ENV['PRODUCTION_URI']
        path
      end
    end
  end
end

2)或者,如果您可以将图像嵌入CSS,则将样式表更改为.erb扩展名,并用url(<%= asset_data_uri "bg.png" %>)替换image-url("bg.png")即可,而无需更改sass-rails. asset-data-uri不作为纯Sass函数存在,因此您必须使用Rails帮助器asset_data_uri.

2) Alternatively if you are ok to embed images in the CSS, changing the stylesheet to have a .erb extension, and replacing the image-url("bg.png") with url(<%= asset_data_uri "bg.png" %>) will work without any need to change sass-rails. asset-data-uri doesn't exist as a pure Sass function so you have to use the Rails helper asset_data_uri.

这篇关于部署到子URI时,Rails 3.1中的预编译资产损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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