问题 Rails资产没有预编译,css在生产中看起来不同


在开发模式下我的rails应用程序工作和看起来完全符合我的要求,但在生产中它在chrome和safari上看起来不同,在safari中徽标图像加载但不是字体,在chrome中加载字体而不是图像加上输入字段有点长,并且在chrome中不对齐,但在开发模式下,它们在chrome中看起来都很棒

我一直搞乱了这一点,并删除了公共/资产几次

rake assets:precompile RAILS_ENV=production 

没有成功,预编译没有错误

配置/ 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'

4634
2018-04-15 22:05


起源

你的生产环境是什么?你在部署到Heroku吗? - winston
不,我在美洲狮上运行的Nitrous.IO上使用自己的盒子 - franklinexpress
你也在运行nginx吗?您可能需要在生产配置中将config.serve_static_assets设置为false。您还可以发布您的徽标和CSS链接的代码吗? - winston
没有运行nginx,如果我在production.rb和application.rb中有重复的设置,这有关系吗? - franklinexpress
请在此处查看我的配置文件: jamestansley.com/2013/06/23/...    。自从我部署到Heroku后,有些设置会有所不同,但请查看我的预编译设置。还要确保在布局中使用正确的CSS样式表链接 - winston


答案:


在你的 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),该服务器旨在直接提供静态文件(包括资产),而无需将请求发送到rails应用服务器。

因为,您没有使用任何Web服务器,我们将其关闭。

  • 通过设置 config.assets.compile = true,我们告诉rails在运行时编译所请求的资产。即查找所请求的资产 app/assetsvendor/assetslib/assets 如果从这些地方发现,请提供服务。

默认, config.assets.compile 是 true 开发中, false 在生产环境中。由于我们没有使用Web服务器来提供静态资产,因此我们要求使用rails来编译我们的资产。

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


11
2017-12-23 19:20



config.assets.compile = true 是我的踢球者。有时,设置的评论并没有真正阐明它的影响。这个更像是告诉rails期望编译资产,而不是真正的轨道 本身 应该编译它们。 - Volte