Cloudfront enables to set up a CDN easily. This module helps to integrate your Cloudfront CDN with play.
Configure a new resolver:
resolvers += "Mariot Chauvin repository" at "http://mchv.me/repository"
Add the library dependency:
libraryDependencies += "mchv" %% "play2-cloudfront" % "1.1"
Create a new file named RemoteAssets.scala
that contains:
package controllers
import controllers._
object RemoteAssets extends Remote {
def call(file:String) = {
controllers.routes.RemoteAssets.at(file)
}
}
Update the router file with the call to your custom controller for your assets:
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.RemoteAssets.at(path="/public", file)
Update your views to refer to your controller to fetch the url:
@(title: String)(content: Html)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel="stylesheet" media="screen" href="@RemoteAssets.url("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@RemoteAssets.url("images/favicon.png")">
<script src="@RemoteAssets.url("javascripts/jquery-1.9.1.min.js")" type="text/javascript"></script>
</head>
<body>
@content
</body>
</html>
Add your Cloudfront url to the application.conf
:
cdn-url="http://d7471vfo50fqt.cloudfront.net"
The module is based on James Ward tutorial If you want to know more or avoid the dependency, please read it.