Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix managed resource not found issue #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/g8/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ lazy val server = project
"com.vmunier" %% "scalajs-scripts" % "1.2.0"
),
Assets / WebKeys.packagePrefix := "public/",
Runtime / managedClasspath += (Assets / packageBin).value
Runtime / unmanagedClasspath += (Assets / packageBin).value
)
.enablePlugins(SbtWeb, SbtTwirl, JavaAppPackaging)
.dependsOn(shared.jvm)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.net.URL
import scala.collection.concurrent.TrieMap
import scala.concurrent.blocking

/**
* Copy from play framework.
*/
object VersionedResourceFinder {
// Sames goes for the minified paths cache.
private lazy val minifiedPathsCache = TrieMap[String, String]()

private def resource(str: String): Option[URL] = Option(
getClass.getClassLoader.getResource(str)
)

private def minifiedPath(path: String): String = {
minifiedPathsCache.getOrElse(
path, {
def minifiedPathFor(delim: Char): Option[String] = {
val ext = path.reverse.takeWhile(_ != '.').reverse
val noextPath = path.dropRight(ext.length + 1)
val minPath = noextPath + delim + "min." + ext
Option(getClass.getResource(minPath)).map(_ => minPath)
}

val maybeMinifiedPath =
minifiedPathFor('.').orElse(minifiedPathFor('-')).getOrElse(path)
minifiedPathsCache.put(path, maybeMinifiedPath)
maybeMinifiedPath
}
)
}

private lazy val digestCache = TrieMap[String, Option[String]]()

lazy val digestAlgorithm: String = "md5"

private def digest(path: String): Option[String] = {
digestCache.getOrElse(
path, {
val maybeDigestUrl: Option[URL] =
resource(path + "." + digestAlgorithm)
val maybeDigest: Option[String] = maybeDigestUrl.map { url =>
val source = scala.io.Source.fromURL(url)
try source.getLines.mkString.trim
finally source.close()
}
if (maybeDigest.isDefined) digestCache.put(path, maybeDigest)
maybeDigest
}
)
}

def findAssetPath(base: String, path: String): String = blocking {
val minPath = minifiedPath(path)
digest(minPath)
.fold(minPath) { dgst =>
val lastSep = minPath.lastIndexOf("/")
minPath.take(lastSep + 1) + dgst + "-" + minPath.drop(lastSep + 1)
}
.drop(base.length + 1)
}
}
6 changes: 5 additions & 1 deletion src/main/g8/server/src/main/twirl/$package$/main.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
</head>
<body>
@content
@scalajs.html.scripts("client", name => s"/assets/$name", name => getClass.getResource(s"/public/$name") != null)
@scalajs.html.scripts("client",
name => {
val versionedJs = VersionedResourceFinder.findAssetPath("public", s"public/$name")
s"/assets/$versionedJs"
}, name => getClass.getResource(s"/public/$name") != null)
</body>
</html>