【问题标题】:Set Charset to UTF-8 on html pages for Middleman blog site在 Middleman 博客网站的 html 页面上将 Charset 设置为 UTF-8
【发布时间】:2014-01-05 20:46:54
【问题描述】:

我在 Heroku (http://tomgillard.herokuapp.com) 上托管了一个 Middleman 博客,并且一直在尝试根据 google 的 PageSpeed 建议对其进行优化。一个建议是我在网站的 HTML 页面上提供一个字符集。

HTML 页面在

中包含 html5 但这似乎还不够,我认为我可以将其设置为服务器端。

这是我的 config.ru

require 'rack/contrib'

# Modified version of TryStatic, from rack-contrib
# https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/try_static.rb

# Serve static files under a `build` directory:
# - `/` will try to serve your `build/index.html` file
# - `/foo` will try to serve `build/foo` or `build/foo.html` in that order
# - missing files will try to serve build/404.html or a tiny default 404 page

module Rack

  class TryStatic

    def initialize(app, options)
      @app = app
      @try = ['', *options.delete(:try)]
      @static = ::Rack::Static.new(lambda { [404, {}, []] }, options)
    end

    def call(env)
      orig_path = env['PATH_INFO']
      found = nil
      @try.each do |path|
        resp = @static.call(env.merge!({'PATH_INFO' => orig_path + path}))
        break if 404 != resp[0] && found = resp
      end
      found or @app.call(env.merge!('PATH_INFO' => orig_path))
    end
  end
end

# Serve GZip files to browsers that support them
use Rack::Deflater

# Custom HTTP Headers
use Rack::ResponseHeaders do |headers|
  headers['Charset'] = 'UTF-8'
end

#Custom Cache Expiry
use Rack::StaticCache, :urls => ["/img", "/css", "/js", "/fonts"], :root => "build"

# Attempt to serve static HTML file
use Rack::TryStatic, :root => "build", :urls => %w[/], :try => ['.html', 'index.html', '/index.html']

# Serve 404 messages:
run lambda{ |env|
  not_found_page = File.expand_path("../build/404.html", __FILE__)
  if File.exist?(not_found_page)
    [ 404, { 'Content-Type'  => 'text/html', 'Charset' => 'UTF-8' },   [File.read(not_found_page)] ]
  else
    [ 404, { 'Content-Type'  => 'text/html', 'Charset' => 'UTF-8' }, ['404 - page not found'] ]
  end
}

我认为我可以使用 rack-contrib 中的 Rack::ResponseHeaders 但我认为我没有正确使用它;

# Custom HTTP Headers
use Rack::ResponseHeaders do |headers|
  headers['Charset'] = 'UTF-8'
end

就像我说的,我从高处和低处搜索过;参考文档(Rack、heroku)、SO 问题、博客文章、github,应有尽有。

非常感谢任何帮助。

干杯, 汤姆

【问题讨论】:

  • “但这似乎还不够”是什么意思?您是否只是在寻找要设置的标头 - 它应该是 Content-Type: text/html; charset=utf-8(即标头名称是 Content-Type,值是 text/html; charset=utf-8)。见w3.org/International/O-HTTP-charset
  • 谢谢@matt。是的,看起来我已将标题设置为不存在的标题(字符集)。如果我将 headers['Charset] = 'utf-8' 更改为 headers['Content-type'] = 'text/html; charset=utf-8' 将 Rack 知道仅将该内容类型应用于 html 文件,还是会在服务器上的每个文件中设置它?如果是后者,我如何只设置带有您上面提到的标题 tpye 和值的 .html 文件?这是我不确定的。

标签: ruby heroku utf-8 rack middleman


【解决方案1】:

我遇到了类似的问题,想优化我的网站。您只需要手动设置Content-Type 标头即可。

这是我完整的config.ru,它在 Google PageSpeed 上达到了 98/100(它抱怨没有在页面中嵌入我的 css):

# encoding: utf-8
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
require 'rack/contrib'

require File.expand_path("../rack_try_static", __FILE__)

use Rack::ResponseHeaders do |headers|
  headers['Content-Type'] = 'text/html; charset=utf-8' if headers['Content-Type'] == 'text/html'
end
use Rack::Deflater
use Rack::StaticCache, urls: ["/images", "/stylesheets", "/javascripts", "/fonts"], root: "build"
use ::Rack::TryStatic,
  root: "build",
  urls: ["/"],
  try: [".html", "index.html", "/index.html"]

run lambda { [404, {"Content-Type" => "text/plain"}, ["File not found!"]] }

您还需要将rack-contrib 添加到您的Gemfile 以获取Rack::StaticCache

gem 'rack-contrib'

你也会想要我的rack_try_static

编辑:请注意,Rack::StaticCache 的当前实现删除了 Last-ModifiedEtag 标头,并将中断以 - 后跟数字结尾的资产。我为这两个(8384)打开了 PR。

【讨论】:

  • 这很棒@Eric。谢谢
  • 没问题。哦,据我所知,您应该从 HTML 文档中删除字符集 meta 元素(仅使用标题)。 Google says 它将“禁用 Internet Explorer 8 中的先行下载器”。此外,如果由于某种原因字符集不匹配,浏览器将呈现错误的页面。
【解决方案2】:

这不再适用,因为我已将我的博客从 heroku 移动到 github 页面。感谢观看。

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 2015-12-11
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多