§Configuring gzip encoding
Play provides a gzip filter that can be used to gzip responses. It can be added to the applications filters using the Global
object. To enable the gzip filter, add the Play filters helpers dependency to your project in build.sbt
:
libraryDependencies += filters
§Enabling gzip in Scala
The simplest way to enable the gzip filter in a Scala project is to use the WithFilters
helper:
import play.api._
import play.api.mvc._
import play.filters.gzip.GzipFilter
object Global extends WithFilters(new GzipFilter()) with GlobalSettings {
// onStart, onStop etc...
}
To control which responses are and aren’t implemented, use the shouldGzip
parameter, which accepts a function of a request header and a response header to a boolean.
For example, the code below only gzips HTML responses:
new GzipFilter(shouldGzip = (request, response) =>
response.headers.get("Content-Type").exists(_.startsWith("text/html")))
§Enabling GZIP in Java
To enable gzip in Java, add it to the list of filters in the Global
object:
import play.GlobalSettings;
import play.api.mvc.EssentialFilter;
import play.filters.gzip.GzipFilter;
public class Global extends GlobalSettings {
public <T extends EssentialFilter> Class<T>[] filters() {
return new Class[]{GzipFilter.class};
}
}
Found an error in this documentation? The source code for this page can be found here. After reading the documentation guidelines, please feel free to contribute a pull request. Have questions or advice to share? Go to our community forums to start a conversation with the community.