Using CDN in Play 2.0

I have a high traffic site with a lot of static content. It is currently in Play 1.2.4, but I am making the transition to Play 2.0.2.

For Play 1.X, we wrote the code that we used instead of @asset inside the html templates.

/**
 * Drop-in replacement for @asset. Use to take advantage of cloudfront on live.
 * Paths are always absolute to root. Leading '/' is optional.
 *
 * @param path relative to the application root. This should usually be "public/some-file"
 * @return path to asset on the currently configured CDN.
 */
def cdnAsset(path: String) : String = {
  cdnEnabled match {
    case "true" =>
      path(0) match {
        case '/' => "https://" + cdnUrl + path
        case _ =>  "https://" + cdnUrl + "/" + path
      }

    case _ =>
        play.mvc.Router.reverse(play.Play.getVirtualFile(path))
  }
}

For Play 2.0, I think we can improve this. I think it would be better if we did not have to select our templates using our special code instead of using @ Asset.at provided in Play 2.0. I'm not sure the best way to do this. I am wondering if something like this was done in response to this question in Play 1.2.X Hosting static HTML in the game! CloudFront applications can be made for Play 2.0.

Assets, Play 2.0, , .

- ? , , , Play, , .

+5
1

, .

+4

All Articles