Documentation

You are viewing the documentation for the 2.4.0-M2 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. 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};
    }
}

Next: Configuring security headers