Embed ETag in URL

2019-03-04 14:51发布

A question about asset fingerprinting in Play.

How to ask Play to embed ETags in URLs without using a third-party plugin?

E.g., if /css/resource.css had the ETag of 1234, then it would become /css/responsive-1234.css.

Related questions: Custom ETag algorithm for asset fingerprinting & Automatically Insert ETag (asset fingerprinting) as comment at top of the resource

1条回答
时光不老,我们不散
2楼-- · 2019-03-04 15:51

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

Then in routes:

  GET  /resources/*file  controllers.Resources.at(file)
查看更多
登录 后发表回答