Play Framework 2.1.3 Do Not Serve GZIP Assets

I use Play Framework 2.1.3 for my site, and I noticed that the built-in web server (Netty) does not serve Gzip assets, although the Play documentation says that it should be automatic, the name with the prefix .gz is present in the shared folder.

I tried Gzipping assets manually (e.g. bootstrap.min.js -> bootstrap.min.js.gz), and the structure will not serve as a .gz version of the file through the Assets controller. I also tried using the following (hacker) code in Build.scala to implement automatic gzipping, but it still doesnโ€™t work at all (and I canโ€™t determine where the gzip files are, even if the log says the assets were actually gzipped):

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 = play.Project(appName, appVersion, appDependencies).settings( // Add your own project settings here resolvers += Resolver.url("sitemapper repository", url("http://blabluble.github.com/modules/releases/"))(Resolver.ivyStylePatterns), gzippableAssets <<= (classDirectory in Compile)(dir => (dir ** ("*.js" || "*.css" || "*.html" || "*.jpg" || "*.png" || "*.jpeg" || "*.gif" || "*.eot" || "*.svg" || "*.ttf" || "*.woff"))), gzipAssetsSetting, playPackageEverything <<= playPackageEverything dependsOn gzipAssets ) 

Why can't Play include GZIP support initially, like all other major web structures, instead of forcing people to crack them together? Using front-end web server for this is not an option for me at present. Any suggestions or simpler ways to do this are greatly appreciated. The documentation for the game is rather scarce in such things.

For those who are interested in checking the absence of serviced assets of GZip, the site http://Netizen.us

+4
source share
1 answer

I suspect that you tested gzip delivery in dev mode and not in prod mode. In dev mode, many optimizations are disabled.

Play 2.2 will have better gzip support: https://github.com/playframework/playframework/pull/1515


I created a test project ( https://github.com/schleichardt/stackoverflow-answers/tree/so18604777 ) and I cannot play it in prod mode using

 sbt dist && cd dist && unzip so18604777-1.0-SNAPSHOT.zip && cd so18604777-1.0-SNAPSHOT && bash start 

I manually created main.css.gz for main.css and got the following response headers in Chromium 28:

 HTTP/1.1 200 OK Vary: Accept-Encoding Last-Modified: Wed, 04 Sep 2013 06:13:32 GMT Etag: "67253bcb7bacb5bbfe3d445f35ae177b60429d5e" Content-Encoding: gzip Content-Length: 49 Cache-Control: max-age=3600 Content-Type: text/css; charset=utf-8 Date: Wed, 04 Sep 2013 06:18:00 GMT 

49 bytes is exactly the size of the gzipped file (original: 20 bytes) and the displayed content was correct.

For a file with different, but with the same contents, that does not have the gzipped equivalent:

 HTTP/1.1 200 OK Last-Modified: Wed, 04 Sep 2013 06:13:32 GMT Etag: "48ccae14d846b77d1a33a1db18f52e0841f0a830" Content-Length: 20 Cache-Control: max-age=3600 Content-Type: text/css; charset=utf-8 Date: Wed, 04 Sep 2013 06:18:25 GMT 
+4
source

Source: https://habr.com/ru/post/1500424/


All Articles