簡體   English   中英

在Middleman博客網站的html頁面上將Charset設置為UTF-8

[英]Set Charset to UTF-8 on html pages for Middleman blog site

我在Heroku( http://tomgillard.herokuapp.com )上托管了一個Middleman博客,並一直在嘗試根據Google的PageSpeed建議對其進行優化。 一個建議是,我在網站的HTML頁面上提供一個字符集。

HTML頁面在<head>中包含html5 <meta charset =“ utf-8”>,但這似乎還不夠,我想我可以在服務器端進行設置。

這是我的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 :: ResponseHeaders的rack-contrib,但我認為我沒有正確使用它。

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

就像我說的那樣,我搜尋了很多東西。 咨詢過的文檔(機架,heroku),問題,博客文章,github,您都可以命名。

非常感謝您提供任何幫助。

干杯湯姆

我有一個類似的問題,想優化我的網站。 您只需要手動設置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-contribGemfileRack::StaticCache

gem 'rack-contrib'

您還需要我的rack_try_static

編輯 :請注意, Rack::StaticCache的當前實現會刪除Last-ModifiedEtag標頭,並且會在以-后跟數字結尾的資產上中斷。 我為這兩個( 8384 )都開放了PR。

由於我已將博客從heroku移至github頁面,因此不再適用。 感謝您的光臨。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM