我希望Play将GZipped JS和CSS发送到浏览器。 在Build.scala中,我添加了
val gzipAssets = TaskKey[Seq[File]]("gzip-assets", "GZIP all assets")
lazy val gzipAssetsSetting = gzipAssets <<= gzipAssetsTask
lazy val gzipAssetsTask = (gzippableAssets, streams) map {
case (finder: PathFinder, s: TaskStreams) => {
finder.get.map { file =>
val gzTarget = new File(file.getAbsolutePath + ".gz")
IO.gzip(file, gzTarget)
s.log.info("Compressed " + file.getAbsolutePath + " " + file.length / 1000 + " k => " + gzTarget.getName + " " + gzTarget.length / 1000 + " k")
gzTarget
}
}
}
val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
// Twitter Bootstrap v2.0.1 compilation (https://plus.google.com/u/0/108788785914419775677/posts/QgyUF9cXPkv)
lessEntryPoints <<= (sourceDirectory in Compile)(base => ((base / "assets" / "stylesheets" / "twitterbootstrap" / "styles.less"))),
// set up gzip of assets
gzippableAssets <<= (resourceManaged in (ThisProject))(dir => ((dir ** "*.js") +++ (dir ** "*.css"))),
gzipAssetsSetting,
playPackageEverything <<= playPackageEverything dependsOn gzipAssets
).settings( ...
当我在dist时生成目标中的文件:
[info] Compressed /Users/wimha/Documents/**/target/scala-2.9.1/resource_managed/main/public/stylesheets/twitterbootstrap/styles.min.css 183 k =&gt; styles.min.css.gz 27 k
但是,在prod中,该文件不可用:
无法加载资源:服务器响应状态为404(未找到)http://ec2-54-228-70-193.eu-west- 1.compute.amazonaws.com/assets/stylesheets/twitterbootstrap/styles.min.css.gz
我有两个问题:
由于 于连
答案 0 :(得分:1)
我为同样的问题寻找解决方案的时间很少。
试试这个:http://play.lighthouseapp.com/projects/82401/tickets/841-gzip-assets。无论您运行.gz
还是play dist
,它都会打包play stage
个文件。
鉴于Play已经足够聪明,可以在a 相应的.gz文件存在,gzip资产何时才有意义 做'舞台'或'分开'。一些反向代理可以gzip资源,但是 这要么效率较低(如果为每个请求完成)或更多 复杂(如果资源被缓存,最终可能会过时)。
这是使用Build.scala的半生不熟的解决方案:
val gzippableAssets = SettingKey[PathFinder]("gzippable-assets", "Defines the files to gzip") val gzipAssets = TaskKey[Seq[File]]("gzip-assets", "gzip all assets") lazy val gzipAssetsSetting = gzipAssets <<= gzipAssetsTask dependsOn (copyResources in Compile) lazy val gzipAssetsTask = (gzippableAssets, streams) map { case (finder: PathFinder, s: TaskStreams) => { var count = 0 var files = finder.get.map { file => val gzTarget = new File(file.getAbsolutePath + ".gz") IO.gzip(file, gzTarget) count += 1; gzTarget } s.log.info("Compressed " + count + " asset(s)") files } } val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings( ... gzippableAssets <<= (classDirectory in Compile)(dir => (dir ** ("*.js" || "*.css" || "*.html"))), gzipAssetsSetting, playPackageEverything <<= playPackageEverything dependsOn gzipAssets )