Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up asset fingerprinting in Play 2.3.4?

Versions:

  • play 2.3.4
  • sbt 0.13.1
  • scala 2.11.2

I've followed the documentation on playframework.com to enable fingerprinting on public assets, but calls to routes.Assets.versioned never produce a versioned filename with a digest hash.

Relevant lines in build.sbt:

scalaVersion := "2.11.2"
pipelineStages := Seq(rjs, digest)

Relevant lines in project/plugins.sbt:

addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.3.4")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.5")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.0.0")

Relevant lines in conf/routes:

GET    /assets/*file    controllers.Assets.versioned(path="/public", file: Asset)

And the main template:

@(title: String, lang: String, cssClasses: String, bodyContents: Html)
<!DOCTYPE html>
<html lang="@lang">
  <head>
    <meta charset="UTF-8">
    <title>@title</title>
    <script type="text/javascript" src="@routes.Assets.versioned("javascript/components/main.js")"></script>
  </head>
  <body>
    <div class="layout @cssClasses">
    @bodyContents
    </div>
  </body>
</html>

The output is always:

<!DOCTYPE html>
<html lang="el">
<head>
  <meta charset="UTF-8">
  <title>[title]</title>
  <script type="text/javascript" src="/assets/javascript/components/main.js"></script>
</head>
<body>
...
</body>
</html>

I get no compiler errors. The fingerprinting just "doesn't work". I assume I am missing something simple, but I cannot see it.

Other notes:

  • running find . -name "*.js" in the root of the project shows no files that have a digest appended to the beginning, as the documentation suggests
  • I've run sbt clean dist to generate a production mode release and the behavior is the same

Can anyone advise?

Thank you!

NB: I've started looking through the generated class in target/src_managed/main/routes_reverseRouting.scala to debug the generated versioned method, but this seems like overkill for something that is fairly straightforward.

like image 247
biegel Avatar asked Sep 08 '14 20:09

biegel


3 Answers

First off, you should update to sbt 0.13.5, because sbt-web and its plugins use an sbt feature called "auto-plugins" that was introduced in 0.13.5.

The asset pipeline is by the way not triggered in development mode, you have to test via sbt start (or sbt dist but that takes more time).

The versioned method just checks if an asset has a companion with the .md5 suffix. You should check if these files exist in target/web.

like image 106
Marius Soutier Avatar answered Nov 18 '22 05:11

Marius Soutier


As far as I can remember I had the same problem a couple of weeks ago. Change the asset route to:

GET   /web/assets/*file   controllers.Assets.versioned(path="/public", file:Asset)

(See the "file:Asset" - it seems that "Asset" is required if I remember correctly)

like image 30
tfh Avatar answered Nov 18 '22 04:11

tfh


If the rjs task of the pipeline encounters some problem, then the digest task may not work properly. For instance, this happens when the default main entry point for rjs is not found:

Error: Error: .../target/web/rjs/appdir/javascripts/main.js does not exist.

In this particular case, setting the sbt key RjsKeys.mainModule to the right value solves the problem and *.js files are properly fingerprinted.

like image 35
jserrano Avatar answered Nov 18 '22 03:11

jserrano