首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用nanoc进行缓存破坏

使用nanoc进行缓存破坏
EN

Stack Overflow用户
提问于 2013-01-25 03:03:15
回答 2查看 293关注 0票数 1

如何使用Nanoc实现缓存崩溃?

例如,将MD5校验和添加到HTML和CSS文件上的所有image/font/js/etc资源链接。例如,如果我有index.htmlimages/badger.jpg,我希望将页面上的图像链接更改为如下所示

代码语言:javascript
复制
`href="images/badger.jpg?12345"`

假设12345将是badger.jpg的正确MD5哈希。

EN

回答 2

Stack Overflow用户

发布于 2013-01-25 17:21:03

你可以采用一种路由方法。我建议使用不同的文件名,而不是querystring -一些http缓存不会缓存带有querystring的urls。

代码语言:javascript
复制
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

您不能在路由中使用生成内容的校验和,因为路由是在编译之前完成的。

票数 2
EN

Stack Overflow用户

发布于 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将确保在您编译站点时,对已提取指纹的文件的引用将会更新。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14508779

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档