Rails 资产未预编译,css 在生产中看起来不同 [英] Rails assets not precompiling, css looks different in production

查看:34
本文介绍了Rails 资产未预编译,css 在生产中看起来不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的开发模式下的 rails 应用程序可以正常工作并且看起来和我想要的完全一样,但是在生产环境中它在 chrome 和 safari 上看起来不同,在 safari 中加载徽标图像但没有加载字体,在 chrome 中加载字体而不加载图像此外,输入字段在 chrome 中稍长且未对齐,但在开发模式下,chrome 中的所有内容看起来都很棒

My rails app in dev mode works and looks exactly as I want it to, but in production it looks different on chrome and safari, in safari the logo images loads but not the font, in chrome the font loads but not the image plus the input fields are a little longer and mis-aligned in chrome but in dev mode it all looks great in chrome

我已经搞砸了一段时间并删除了公共/资产几次

I been messing with this for a while and deleted the public/assets a few times a did

rake assets:precompile RAILS_ENV=production 

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

with no success, the precompile goes through with no errors

config/application.rb:

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'

推荐答案

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

In your config/environments/production.rb file, set:

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

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

这应该可以解决您的问题.

This should solve your problem.

让我解释一下我要你做什么.

Let me explain what I am asking you to do.

  • 通过设置config.serve_static_assets = false,我们告诉rails服务器,不要添加ActionDispatch::Static中间件,它用于提供静态资源.
  • By setting config.serve_static_assets = false, we are telling rails server, don't add ActionDispatch::Static middleware, which is used to serve static assets.

为什么不呢?

那是因为在生产环境中,您需要在 Web 服务器(例如 Apache/Nginx)后面运行您的应用程序服务器(例如 puma),该服务器旨在直接提供静态文件(包括资产),而无需费心发送请求 Rails 应用服务器.

That's because in production environment, you are expected to run your application server (like puma) behind a web server (like Apache/Nginx), which is designed to serve static files (including assets) directly, without bothering to send the request to rails application server.

由于您没有使用任何网络服务器,我们将其关闭.

Since, you are not using any web server, we are turning it off.

  • 通过设置 config.assets.compile = true,我们告诉 rails 在运行时编译请求的资源.即在 app/assetsvendor/assetslib/assets 中查找请求的资产,如果从这些位置中的任何一个找到,则提供它.
  • By setting config.assets.compile = true, we are telling rails to compile the requested asset at runtime. i.e. Look for the requested asset in app/assets, vendor/assets, lib/assets and serve it if found from any of these locations.

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

By default, config.assets.compile is true in development, false in production environment. Since, we are not using web server to serve static assets, we are asking rails to live compile our assets.

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

For further details refer to the asset pipeline documentation.

这篇关于Rails 资产未预编译,css 在生产中看起来不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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