在Middleman博客站点的html页面上将Charset设置为UTF-8

时间:2014-01-05 20:46:55

标签: ruby heroku utf-8 rack middleman

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

HTML网页包含html5< meta charset =“utf-8”>在< head>中但这似乎不足以让我认为我可以设置服务器端。

这是我的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
像我说的那样,我搜索过高低;咨询了docs(Rack,heroku),SO问题,博客文章,github,你的名字。

非常感谢任何帮助。

干杯, 汤姆

2 个答案:

答案 0 :(得分: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的{​​{1}}:

Rack::StaticCache

您还需要我的rack_try_static

修改:请注意gem 'rack-contrib' 的当前实施会删除Rack::StaticCacheLast-Modified标题,并且会以以 - 后跟数字结尾的资产中断。我为这两个(8384)打开了公关。

答案 1 :(得分:0)

这已不再适用,因为我已将我的博客从heroku移至github页面。谢谢你的期待。