Rails资产未预编译,生产环境下CSS外观不同。

13

我的Rails开发模式下的应用程序在外观和功能上都符合我的要求,但是在生产环境中,在Chrome和Safari中看起来不同。在Safari中,标志图像加载但是字体没有加载;在Chrome中,字体加载但是图像没有加载,并且输入字段在Chrome中略微变长且排列不齐,但是在开发模式下,Chrome中所有内容看起来都非常棒。

我已经研究了一段时间,并删除了公共/资产几次。

rake assets:precompile RAILS_ENV=production 

尝试无果后,预编译通过且没有错误。

config/application.rb:

 # Settings in config/environments/* take precedence over those specified here.
 # Application configuration should go into files in config/initializers
 # -- all .rb files in that directory are automatically loaded.
 config.assets.paths << "#{Rails.root}/assets/fonts"
 config.assets.paths << "#{Rails.root}/assets/images"
 config.assets.paths << Rails.root.join("app", "assets", "fonts")
 config.assets.precompile += %w( .svg .eot .woff .ttf )


# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.assets.enabled = true  
#config.assets.paths << "#{Rails.root}/app/assets/fonts" 

配置/环境/生产:

# Code is not reloaded between requests.
config.cache_classes = true

# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both thread web servers
# and those relying on copy on write to perform better.
# Rake tasks automatically ignore this option for performance.
config.eager_load = true

# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local       = false
config.action_controller.perform_caching = true

# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx,  varnish or squid.
# config.action_dispatch.rack_cache = true

# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
#config.assets.compile = true

config.assets.precompile =  ['*.js', '*.css', '*.css.erb', '*.css.scss'] 
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
config.assets.paths << "#{Rails.root}/assets/fonts"
config.assets.paths << "#{Rails.root}/assets/images"
config.assets.precompile += %w( .svg .eot .woff .ttf )
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
config.assets.precompile << /(^[^_\/]|\/[^_])[^\/]*$/
# Generate digests for assets URLs.
config.assets.digest = true

# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'

1
你们的生产环境是什么?你们在部署到Heroku吗? - winston
我正在使用Nitrous.IO上的自己的盒子,运行在puma上。 - franklinexpress
1
你也在运行nginx吗?你可能需要在生产配置中将config.serve_static_assets设置为false。另外,你能否请发布你的标志和CSS链接的代码? - winston
没有运行nginx,如果我在production.rb和application.rb中有重复的设置,这会有影响吗? - franklinexpress
1
请随意查看我的配置文件:http://jamestansley.com/2013/06/23/troubleshooting-the-rails-asset-pipeline-for-cloud-application-platforms/。由于我部署到Heroku,因此某些设置可能会有所不同,但请查看我的预编译设置。同时,请确保在您的布局中使用正确的CSS样式表链接。 - winston
1个回答

14

在你的config/environments/production.rb文件中,设置:

config.serve_static_assets = false(当前设置为true)

config.assets.compile = true(当前设置为false)

这样应该就能解决你的问题。

让我解释一下我要求你做什么。

  • 通过设置config.serve_static_assets = false,我们告诉Rails服务器不要添加ActionDispatch::Static中间件,该中间件用于提供静态资源。

为什么不要呢?

因为在生产环境中,你需要在Web服务器(如Apache/Nginx)后面运行你的应用服务器(如Puma),Web服务器可以直接提供静态文件(包括资产),而无需发送请求到Rails应用服务器。

由于你没有使用任何Web服务器,所以我们要关闭它。

  • 通过设置config.assets.compile = true,我们告诉Rails在运行时编译请求的资产。也就是说,在app/assetsvendor/assetslib/assets中查找请求的资产,并从这些位置中的任何一个位置找到其服务。

默认情况下,在开发环境中config.assets.compiletrue,在生产环境中为false。由于我们没有使用Web服务器来提供静态资源,所以我们要求Rails实时编译我们的资源。

有关详细信息,请参阅资产管道文档


对我来说,“config.assets.compile = true”是关键。有时候,与设置相关的注释并不能真正阐明其影响。这个设置实际上更像是告诉Rails要期望编译后的资源,而不是让Rails本身去编译它们。 - Volte

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接