1

Nanoc でキャッシュ無効化を実装するには?

たとえば、HTML および CSS ファイルのすべての image/font/js/etc リソース リンクに MD5 チェックサムを追加します。たとえば、 と があった場合、ページの画像リンクを次のように変更したいと思いindex.htmlますimages/badger.jpg

`href="images/badger.jpg?12345"`

12345 が Badger.jpg の正しい MD5 ハッシュであると仮定します。

4

2 に答える 2

2

Arjan van der Gaag は、これ専用の gem を作成しました: https://github.com/avdgaag/nanoc-cachebuster

彼自身を引用するには

gem をインストールするだけなので、使い方は簡単です。

$ gem install nanoc-cachebuster 

ジェムを要求し、ヘルパーを含めて作業を開始します。

# in default.rb
require 'nanoc3/cachebuster' 
include Nanoc3::Helpers::CacheBusting 

ルーティング ルールで #fingerprint メソッドを使用できるようになりました。

route '/assets/styles/' do
  item.identifier.chop + fingerprint(item) + '.' + item[:identifier]
end

gem は、フィンガープリントを取得したファイルへの参照が、サイトをコンパイルするときに更新されるようにします。

于 2013-04-26T17:46:33.507 に答える
2

ルーティング アプローチを使用できます。クエリ文字列の代わりに実際に異なるファイル名を使用することをお勧めします - 一部の http キャッシュはクエリ文字列で URL をキャッシュしません。

route '/stylesheet/' do
  csum = [File.open(item[:filename]).read.checksum]
  # add other files you include from your stylesheet.less (if you use less)
  csum += Dir['content/styles/*'].select { |i| File.file?(i) }.map { |f| File.read(f).checksum }
  '/style-' + csum.checksum + '.css'
end

route '*' do
  ext = item[:extension]
  versionexts = ['css','js']

  if versionexts.include?(ext)
    # versioned filenames, depending on the checksum of the source file
    # these files shouldn't depend on other sources, or you have to checksum them too (see above)
    item.identifier.chop + '-' + File.read(item[:filename]).checksum + '.' + ext
  elsif item.binary?
    # Write item with identifier /foo/ to /foo.ext
    item.identifier.chop + '.' + ext
  else
    # Write item with identifier /foo/ to /foo/index.html
    item.identifier + 'index.html'
  end
end

ルーティングはコンパイル前に行われるため、生成されたコンテンツのチェックサムをルーティングに使用することはできません。

于 2013-01-25T09:21:02.860 に答える