在 URL 中嵌入 ETag [英] Embed ETag in URL
问题描述
关于 Play 中资产指纹识别的问题.
A question about asset fingerprinting in Play.
如何让 Play 在不使用第三方插件的情况下在 URL 中嵌入 ETag?
How to ask Play to embed ETags in URLs without using a third-party plugin?
例如,如果 /css/resource.css
有 1234
的 ETag,那么它会变成 /css/responsive-1234.css
.
E.g., if /css/resource.css
had the ETag of 1234
, then it would become /css/responsive-1234.css
.
相关问题:资产指纹识别的自定义ETag算法 &自动插入 ETag(资产指纹识别)为在资源顶部评论
Related questions: Custom ETag algorithm for asset fingerprinting & Automatically Insert ETag (asset fingerprinting) as comment at top of the resource
推荐答案
将有出"路由和入"反向路由.首先是出"路由:
There'll be the "out" routing and the "in" reverse routing. Firstly the "out" routing:
package controllers
import play.api.mvc._
object Resources extends Controller {
def fingerprintedUrl(path: String): String = {
// calculates ETag and add to URL path
val pathWithEtag = addEtagToPath(path)
pathWithEtag
}
}
然后在模板中使用以下示例:
Then in templates use the following for example:
<script src="@Resources.fingerprintedUrl("js/toolkit.js")"></script>
现在是反向路由,在同一个控制器中添加以下内容:
Now the reverse routing, add the following to the same controller:
def at(path: String): Action = {
val pathWithoutEtag = removeEtagFromPath(path)
Assets.at("/public", pathWithoutEtag)
}
然后在routes
:
GET /resources/*file controllers.Resources.at(file)
这篇关于在 URL 中嵌入 ETag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!