You are viewing the documentation for the 2.6.0-M4 development release. The latest stable release series is 3.0.x.
§Configuring gzip encoding
Play provides a gzip filter that can be used to gzip responses.
§Enabling the gzip filter
To enable the gzip filter, add the filter to application.conf:
play.filters.enabled += "play.filters.gzip.GzipFilter"
§Configuring the gzip filter
The gzip filter supports a small number of tuning configuration options, which can be configured from application.conf. To see the available configuration options, see the Play filters reference.conf.
§Controlling which responses are gzipped
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:
- Scala
-
new GzipFilter(shouldGzip = (request, response) => response.body.contentType.exists(_.startsWith("text/html"))) - Java
-
GzipFilterConfig gzipFilterConfig = new GzipFilterConfig(); GzipFilter gzipFilter = new GzipFilter( gzipFilterConfig.withShouldGzip((BiFunction<Http.RequestHeader, Result, Object>) (req, res) -> res.body().contentType().orElse("").startsWith("text/html") ), materializer );