createGzipMiddleware function
Middleware
createGzipMiddleware({})
Converts a Response
to a gzip
encoding
(only if the Request
acceptsGzipEncoding).
minimalGzipContentLength
is the minimal size for a content to be compressed. Default to512
.alreadyCompressedContentType
is a function that returnstrue
if the passedcontentType
is already compressed, like aPNG
,JPEG
orZip
. Defaults to isAlreadyCompressedContentType.- The
compressionLevel
for thegzip
encoder. Default: 4 - If
addCompressionRatioHeader
istrue
, add headerX-Compression-Ratio
. - If
addServerTiming
istrue
, include or append Gzip encoding timing to theserver-timing
header. serverTimingEntryName
is the entry name to be used in theserver-timing
header.
Implementation
Middleware createGzipMiddleware({
int minimalGzipContentLength = _defaultMinimalGzipContentLength,
_AlreadyCompressedContentType? alreadyCompressedContentType,
int compressionLevel = _defaultGzipCompressionLevel,
bool addCompressionRatioHeader = true,
bool addServerTiming = false,
String serverTimingEntryName = 'gzip',
}) {
return (Handler innerHandler) {
return (request) {
if (!acceptsGzipEncoding(request)) {
return innerHandler(request);
}
return Future.sync(() => innerHandler(request))
.then((response) => gzipEncodeResponse(
response,
minimalGzipContentLength: minimalGzipContentLength,
alreadyCompressedContentType: alreadyCompressedContentType,
compressionLevel: compressionLevel,
addCompressionRatioHeader: addCompressionRatioHeader,
addServerTiming: addServerTiming,
serverTimingEntryName: serverTimingEntryName,
));
};
};
}