Static Assets
- Play is optimized for caching proxies
- 304 - Not Modified
- Asset Fingerprinting
CDNs & Caching Proxies
def getUrl(file: String) = {
Play.configuration.getString("contenturl") match {
case Some(contentUrl) => contentUrl + routes.RemoteAssets.getAsset(file).url
case None => controllers.routes.RemoteAssets.getAsset(file)
}
}
Reverse Router Wrapped
<script src='@RemoteAssets.getUrl("jquery.min.js")'></script>
Last Modified & ETag
- Out of the box
- Still hits the server
Asset Fingerprinting
The hacky way:
object StaticAssets extends Controller {
val versionStamp: String = new Date().getTime.toString + "/"
def at(file: String) = CustomNotFound {
val actualFile = file.replaceAll(versionStamp, "")
Assets.at("/public", actualFile)
}
def getUrl(file: String) = {
val versionedFile = versionStamp + file
controllers.routes.StaticAssets.at(versionedFile)
}
}
Far Future Expires
result.withHeaders("Cache-Control" -> "max-age=290304000, public")