破壞如何實現緩存,Nanoc破壞?緩存與nanoc
例如,增加MD5校驗所有圖像/字體的HTML和CSS文件/ JS /等資源鏈接。舉例來說,如果我有index.html
和images/badger.jpg
,我想在頁面上的圖片鏈接改變爲類似
`href="images/badger.jpg?12345"`
假設12345是badger.jpg的正確的MD5哈希值。
破壞如何實現緩存,Nanoc破壞?緩存與nanoc
例如,增加MD5校驗所有圖像/字體的HTML和CSS文件/ JS /等資源鏈接。舉例來說,如果我有index.html
和images/badger.jpg
,我想在頁面上的圖片鏈接改變爲類似
`href="images/badger.jpg?12345"`
假設12345是badger.jpg的正確的MD5哈希值。
你可以走了路由方法。我建議,而不是使用查詢字符串實際上是不同的文件名 - 一些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
由於路由在編譯之前完成,所以不能在路由中使用生成內容的校驗和。
阿爾揚·範德GAAG做了寶石專門爲此:https://github.com/avdgaag/nanoc-cachebuster
用法很簡單,因爲你只需要安裝寶石:
$ 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
寶石將確保你已經採集指紋文件的引用將得到更新,當您編譯 網站。
現在,如果我有html,css文件中的版本化css,js,png文件引用,那麼nanoc會幫助它並更新引用以匹配這些路由?那將是真棒。 – raimohanska