[英]How do I get the digest for a Sprockets generated resource?
When using sprockets, I have a whole directory of .coffee files that transpile, merge together and become 'mobile_v2.js'. 使用链轮时,我有一个完整的.coffee文件目录,这些文件可以转换,合并在一起并成为“ mobile_v2.js”。 When this file gets served up a wonderful ETag is set on it with the SHA1 hash of the content.
当该文件送达后,将使用内容的SHA1哈希在其上设置一个奇妙的ETag。 This allows cache-busting and everything else wonderful and good to work correctly.
这样可以清除缓存,其他所有奇妙的东西也可以正常工作。 ie, if I modify any of the source .coffee files and reload the page, Sprockets will regenerate 'mobile_v2.js' and send it down to the client.
即,如果我修改任何源.coffee文件并重新加载页面,则Sprockets将重新生成“ mobile_v2.js”并将其发送给客户端。 If I don't change anything, the client will use it's cached copy of that resource.
如果我不做任何更改,客户端将使用该资源的缓存副本。 This works great.
这很好。
Now I've got a scenario where a portion of the web app uses Backbone.js and the user can spend a really long time on one page without doing a full "refresh" ... ie, lots and lots of AJAX updates, but no new "...". 现在,我有一个场景,其中一部分Web应用程序使用Backbone.js ,并且用户可以在一页上花费很长的时间,而无需进行完整的“刷新” ...即,很多AJAX更新,但是没有新的 ”...”。 Literally, the client could spend weeks on this page.
从字面上看,客户可以在此页面上花费数周。 Thus if I push an update to the JS/CSS resources, I need a way for the client to detect this and trigger a reload of the overall page.
因此,如果我将更新推送到JS / CSS资源,则需要一种方法让客户端检测到此情况并触发整个页面的重新加载。 Doing a full reload is disruptive, so I don't want to do it more often than necessary.
进行完全重新加载会造成破坏,因此,我不想经常执行此操作。 I also don't want to poll the server for these resources ... I have all these AJAX calls, so I can just piggy-back those calls with an additional response header.
我也不想轮询服务器上的这些资源...我拥有所有这些AJAX调用,因此我可以使用附加的响应头将这些调用背负下来。 All that I can do.
我所能做的。 I just need help with one simple question:
我只需要一个简单的问题的帮助:
Given a generated resource like 'mobile_v2.js', how do I query Sprockets to get the SHA1 digest for that file? 给定生成的资源(例如“ mobile_v2.js”),如何查询Sprockets以获取该文件的SHA1摘要? (from my ruby code)
(来自我的红宝石代码)
Not sure if I got you right, but if you're precompiling your assets, one way would be to extract the digest from the generated manifest.yml 不知道我是否正确,但是如果您正在预编译资产,一种方法是从生成的manifest.yml中提取摘要。
manifest = YAML.load(File.open(Rails.root.join("public", "assets", "manifest.yml")))
digest = manifest["mobile_v2.js"].gsub("mobile_v2-", "").gsub(".js", "")
To find the sprockets digest of a generated asset: 要查找已生成资产的链轮摘要:
Rails.application.assets.find_asset(asset_name).digest
In production, there's no sprockets(*), so we use the git revision (we deploy with the .git folder). 在生产中,没有链轮(*),因此我们使用git修订版(使用.git文件夹进行部署)。 Alternatively, I guess we could have read the manifest.yaml as suggested by DavidO.
另外,我想我们可以阅读DavidO建议的manifest.yaml。
Here's the full solution I ended up using: 这是我最终使用的完整解决方案:
def asset_digest(asset_name)
if Rails.env == 'production'
# Production
Git.open(Rails.root).object('HEAD').sha
else
# Development / Test
Rails.application.assets.find_asset(asset_name).digest
end
end
(*): Actually, in production, the sprockets line "worked", but it did so by recompiling all of the assets instead of reading the precompiled asset. (*):实际上,在生产中,链轮行“有效”,但是它是通过重新编译所有资产而不是读取预编译的资产来完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.