Documentation

You are viewing the documentation for the 2.2.1 release in the 2.2.x series of releases. 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.

§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};
    }
}