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

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

问题描述

我的rails应用程序在dev模式的工作原理和外观完全一样,我想它,但在生产中看起来不同的chrome和safari,safari标志图像加载,但不是字体,在chrome的字体加载,但不是图像加上输入字段有点长,在chrome中不对齐,但在开发模式下,它在chrome中看起来很不错。



我一直在搞这个一会儿,删除了公共/资产几次a

  rake assets:precompile RAILS_ENV = production 



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



config / application.rb:

 #config / environments / *中的设置优先于这里指定的。 
#应用程序配置应该进入config / initializers文件
# - 该目录中的所有.rb文件将自动加载。
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)

b $ b#将Time.zone默认设置为指定的区域,并使Active Record自动转换到该区域。
#运行rake -D time获取查找时区名称的任务列表。默认值为UTC。
#config.time_zone ='中央时间(美国和加拿大)'

#默认语言环境是:en以及来自config / locales / *。rb,yml的所有翻译都是自动加载的。
#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

config / environments / production: / p>

 #在请求之间不重新加载代码。 
config.cache_classes = true

#启动时加载代码。这个渴望加载大多数Rails和
#你的应用程序在内存中,允许两个线程网络服务器
#和依赖于copy on write的性能更好。
#Rake任务自动忽略此选项的性能。
config.eager_load = true

#完全错误报告被禁用,缓存被打开。
config.consider_all_requests_local = false
config.action_controller.perform_caching = true

#启用Rack :: Cache在应用程序前面放置一个简单的HTTP缓存
#在启用它之前,将`rack-cache`添加到Gemfile。
#对于大规模生产使用,请考虑使用缓存逆向代理,如nginx,varnish或squid。
#config.action_dispatch.rack_cache = true

#禁用Rails的静态资产服务器(Apache或nginx已经这样做了)。
config.serve_static_assets = true
#config.assets.compile = true

config.assets.precompile = ['* .js','* .css','* .css.erb','* .css.scss']
#压缩JavaScript和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')
#如果预编译的资产丢失,不要回退到资产管道。
config.assets.compile = false
config.assets.precompile<< /(^ [^ _ \ /] | \ / [^ _])[^ \ /] * $ /
#为资产URL生成摘要。
config.assets.digest = true

#您的资产版本,如果您想要使所有资产过期,请更改此项。
config.assets.version ='1.0'


解决方案

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



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



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



这应该可以解决你的问题。 b
$ b

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




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



为什么不?



这是因为在生产环境中,希望在Web服务器(如Apache / Nginx)后面运行您的应用程序服务器(如puma),该服务器旨在直接提供静态文件(包括资产),而无需将请求发送到rails应用程序服务器。



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




  • config.assets.compile = true ,我们告诉rails在运行时编译请求的资产。即在 app / assets 供应商/资产 lib / assets中查找请求的资产,并在其中任何位置找到。



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



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


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:

 # 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" 

config/environments/production:

# 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'

解决方案

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

config.serve_static_assets = false (currently it's set to true)

config.assets.compile = true (currently it's set to false)

This should solve your problem.

Let me explain what I am asking you to do.

  • 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.

Why not?

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.

  • 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.

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天全站免登陆