如何使用Nanoc实现缓存崩溃?
例如,将MD5校验和添加到HTML和CSS文件上的所有image/font/js/etc资源链接。例如,如果我有index.html和images/badger.jpg,我希望将页面上的图像链接更改为如下所示
`href="images/badger.jpg?12345"`假设12345将是badger.jpg的正确MD5哈希。
发布于 2013-01-25 17:21:03
你可以采用一种路由方法。我建议使用不同的文件名,而不是querystring -一些http缓存不会缓存带有querystring的urls。
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-04-27 01:46:34
Arjan van der Gaag专门为此制作了一个宝石:https://github.com/avdgaag/nanoc-cachebuster
To quote the man himself
的使用很简单,因为您只需要安装gem:
$ gem安装nanoc cachebuster
并且需要gem并包含helpers才能开始:在default.rb中需要'nanoc3/cachebuster‘包括Nanoc3::Helpers::can现在您可以在路由规则中使用#指纹方法:
路由'/assets/styles/‘执行item.identifier.chop +指纹(项)+’.+项:标识符结束
gem将确保在您编译站点时,对已提取指纹的文件的引用将会更新。
https://stackoverflow.com/questions/14508779
复制相似问题