HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulewai-extra-3.1.16Haskell2010

Network.Wai.Middleware.Gzip

Automatic gzip compression of responses.

  • 2 types
  • 6 values

How to use this module

0 declarations

This Middleware adds gzip encoding to an application. Its use is pretty straightforward, but it's good to know how and when it decides to encode the response body.

A few things to keep in mind when using this middleware:

  • It is advised to put any Middlewares that change the response behind this one, because it bases a lot of its decisions on the returned response.

  • Enabling compression may counteract zero-copy response optimizations on some platforms.

  • This middleware is applied to every response by default. If it should only encode certain paths, Network.Wai.Middleware.Routed might be helpful.

The Middleware

There are a good amount of requirements that should be fulfilled before a response will actually be gzip encoded by this Middleware, so here's a short summary.

Request requirements:

  • The request needs to accept "gzip" in the "Accept-Encoding" header.

  • Requests from Internet Explorer 6 will not be encoded. (i.e. if the request's "User-Agent" header contains "MSIE 6")

Response requirements:

  • The response isn't already encoded. (i.e. shouldn't already have a "Content-Encoding" header)

  • The response isn't a 206 Partial Content (partial content should never be compressed)

  • If the response contains a "Content-Length" header, it should be larger than the gzipSizeThreshold.

  • The "Content-Type" response header's value should evaluate to True when applied to gzipCheckMime (though GzipPreCompressed will use the ".gz" file regardless of MIME type on any ResponseFile response)

The Settings

If you would like to use the default settings, using just def is enough. The default settings don't compress file responses, only builder and stream responses, and only if the response passes the MIME and length checks. (cf. defaultCheckMime and gzipSizeThreshold)

To customize your own settings, use the def method and set the fields you would like to change as follows:

myGzipSettings :: GzipSettings
myGzipSettings =
  defaultGzipSettings
    { gzipFiles = GzipCompress
    , gzipCheckMime = myMimeCheckFunction
    , gzipSizeThreshold = 860
    }
datadata GzipSettings
#
Instances1Default
  • Default GzipSettingsDefined in wai-extra-3.1.16 · Network.Wai.Middleware.Gzip

    Use default MIME settings; do not compress files; skip compression on data smaller than 860 bytes.

Decide which files to compress based on MIME type

The ByteString is the value of the "Content-Type" response header and will default to False if the header is missing.

E.g. if you'd only want to compress json data, you might define your own function as follows:

myCheckMime mime = mime == "application/json"

Skip compression when the size of the response body is below this amount of bytes (default: 860.)

Setting this option to less than 150 will actually increase the size of outgoing data if its original size is less than 150 bytes.

This will only skip compression if the response includes a "Content-Length" header AND the length is less than this threshold.

How to handle file responses

datadata GzipFiles
#

Gzip behavior for files.

Constructors

Instances3Eq, Read, Show
  • Eq GzipFilesDefined in wai-extra-3.1.16 · Network.Wai.Middleware.Gzip
  • Read GzipFilesDefined in wai-extra-3.1.16 · Network.Wai.Middleware.Gzip
  • Show GzipFilesDefined in wai-extra-3.1.16 · Network.Wai.Middleware.Gzip

Miscellaneous

def is re-exported for convenience sake, and defaultCheckMime is exported in case anyone wants to use it in defining their own gzipCheckMime function.

MIME types that will be compressed by default: text/ *, application/json, application/javascript, application/ecmascript, image/x-icon.

methoddef :: a
#

The default value for this type.