在URL中嵌入ETag

时间:2014-08-14 14:28:37

标签: scala playframework

关于Play中资产指纹识别的问题。

如何让Play在不使用第三方插件的情况下在网址中嵌入ETag?

例如,如果/css/resource.css的ETag为1234,那么它将变为/css/responsive-1234.css

相关问题:Custom ETag algorithm for asset fingerprinting& Automatically Insert ETag (asset fingerprinting) as comment at top of the resource

1 个答案:

答案 0 :(得分:0)

将出现“out”路由和“in”反向路由。首先是“出局”路线:

  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
    }
  }

然后在模板中使用以下代码:

 <script src="@Resources.fingerprintedUrl("js/toolkit.js")"></script>

现在反向路由,将以下内容添加到同一个控制器:

  def at(path: String): Action = {
    val pathWithoutEtag = removeEtagFromPath(path)
    Assets.at("/public", pathWithoutEtag)
  }

然后在routes

  GET  /resources/*file  controllers.Resources.at(file)