From 54494bdeea50cde209c81305f405f8bdf0d156d6 Mon Sep 17 00:00:00 2001 From: Vincent Munier Date: Wed, 3 May 2017 12:21:02 +0100 Subject: [PATCH] Use a JavaScript Router supporting Versioned Assets. --- build.sbt | 4 ++++ client/src/main/scala/example/ScalaJSExample.scala | 2 ++ server/app/controllers/Application.scala | 2 +- server/app/views/index.scala.html | 2 +- server/app/views/main.scala.html | 14 +++++++++++++- server/public/javascripts/versioned.js | 1 + 6 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 server/public/javascripts/versioned.js diff --git a/build.sbt b/build.sbt index 909480c..1de8ee7 100644 --- a/build.sbt +++ b/build.sbt @@ -11,6 +11,10 @@ lazy val server = (project in file("server")).settings( "com.vmunier" %% "scalajs-scripts" % "1.0.0", specs2 % Test ), + // Create a map of versioned assets, replacing the empty versioned.js + DigestKeys.indexPath := Some("javascripts/versioned.js"), + // Assign the asset index to a global versioned var + DigestKeys.indexWriter ~= { writer => index => s"var versioned = ${writer(index)};" }, // Compile the project before generating Eclipse files, so that generated .scala or .class files for views and routes are present EclipseKeys.preTasks := Seq(compile in Compile) ).enablePlugins(PlayScala). diff --git a/client/src/main/scala/example/ScalaJSExample.scala b/client/src/main/scala/example/ScalaJSExample.scala index c78acad..39a2c80 100644 --- a/client/src/main/scala/example/ScalaJSExample.scala +++ b/client/src/main/scala/example/ScalaJSExample.scala @@ -4,9 +4,11 @@ import org.scalajs.dom import shared.SharedMessages import scala.scalajs.js +import js.Dynamic.{global => g} object ScalaJSExample extends js.JSApp { def main(): Unit = { + println("Versioned favicon : " + g.jsRoutes.controllers.Assets.versioned("images/favicon.png").url) dom.document.getElementById("scalajsShoutOut").textContent = SharedMessages.itWorks } } diff --git a/server/app/controllers/Application.scala b/server/app/controllers/Application.scala index ff52551..47d48d3 100644 --- a/server/app/controllers/Application.scala +++ b/server/app/controllers/Application.scala @@ -5,7 +5,7 @@ import shared.SharedMessages class Application extends Controller { - def index = Action { + def index = Action { implicit request => Ok(views.html.index(SharedMessages.itWorks)) } diff --git a/server/app/views/index.scala.html b/server/app/views/index.scala.html index 5d9bca0..fb838e8 100644 --- a/server/app/views/index.scala.html +++ b/server/app/views/index.scala.html @@ -1,4 +1,4 @@ -@(message: String) +@(message: String)(implicit req: play.api.mvc.RequestHeader) @main("Play with Scala.js") {

Play and Scala.js share a same message

diff --git a/server/app/views/main.scala.html b/server/app/views/main.scala.html index 5a31f0c..4f1d13a 100644 --- a/server/app/views/main.scala.html +++ b/server/app/views/main.scala.html @@ -1,4 +1,4 @@ -@(title: String)(content: Html) +@(title: String)(content: Html)(implicit req: play.api.mvc.RequestHeader) @@ -7,6 +7,18 @@ @title + + @helper.javascriptRouter("jsRoutes")(routes.javascript.Assets.versioned) + + + @content diff --git a/server/public/javascripts/versioned.js b/server/public/javascripts/versioned.js new file mode 100644 index 0000000..67316a5 --- /dev/null +++ b/server/public/javascripts/versioned.js @@ -0,0 +1 @@ +var versioned = {};