§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
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
You can configure the compression level by using
play.filters.gzip.compressionLevel. The values must be between
9, inclusive, and they follow the semantics defined by
java.util.zip.Deflater. For example, the default configuration is
-1, which is the default compression level and
9 is the best compression. For example:
play.filters.gzip.compressionLevel = 9
§Controlling which responses are gzipped
You can control which responses are and aren’t gzipped based on their content types via
As a more flexible alternative you can use the
shouldGzip parameter of the gzip filter itself, 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.body.contentType.exists(_.startsWith("text/html")))
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 );
Next: Configuring security headers
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.