§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
You can control which responses are and aren’t gzipped based on their content types via application.conf
:
play.filters.gzip {
contentType {
# If non empty, then a response will only be compressed if its content type is in this list.
whiteList = [ "text/*", "application/javascript", "application/json" ]
# The black list is only used if the white list is empty.
# Compress all responses except the ones whose content type is in this list.
blackList = []
}
}
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:
- 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);
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.