Skip to content

Commit

Permalink
Use a JavaScript Router supporting Versioned Assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmunier committed May 3, 2017
1 parent 70fa148 commit 54494bd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions client/src/main/scala/example/ScalaJSExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion server/app/controllers/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
2 changes: 1 addition & 1 deletion server/app/views/index.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(message: String)
@(message: String)(implicit req: play.api.mvc.RequestHeader)

@main("Play with Scala.js") {
<h2>Play and Scala.js share a same message</h2>
Expand Down
14 changes: 13 additions & 1 deletion server/app/views/main.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(title: String)(content: Html)
@(title: String)(content: Html)(implicit req: play.api.mvc.RequestHeader)

<!DOCTYPE html>

Expand All @@ -7,6 +7,18 @@
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">

@helper.javascriptRouter("jsRoutes")(routes.javascript.Assets.versioned)
<script src="@routes.Assets.versioned("javascripts/versioned.js")" type="text/javascript"></script>
<script type="text/javascript">
(function() {
var oldVersioned = jsRoutes.controllers.Assets.versioned;
jsRoutes.controllers.Assets.versioned = function(path) {
return oldVersioned.apply(jsRoutes.controllers.Assets, [versioned[path] || path]);
};
})();
</script>

</head>
<body>
@content
Expand Down
1 change: 1 addition & 0 deletions server/public/javascripts/versioned.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var versioned = {};

0 comments on commit 54494bd

Please sign in to comment.