on master help sign in
only gzip responses < 860 bytes
commitd012aec29c418b78488681c709001cb3e50a0f0e authorGreg Weber <greg@gregweber.info> dateFri 16 Oct 2015, 12:39 (11 years ago) parente2e135f
1 file changed+12−1
wai-extra/Network/Wai/Middleware/Gzip.hs+12−1
wai-extra/Network/Wai/Middleware/Gzip.hs+12−1
@@ -33,6 +33,7 @@ import System.Directory (doesFileExist, createDirectoryIfMissing)
3333import Blaze.ByteString.Builder (fromByteString)
3434import Control.Exception (try, SomeException)
3535import qualified Data.Set as Set
36+import Network.Wai.Header
3637import Network.Wai.Internal
3738import qualified Data.Streaming.Blaze as B
3839import qualified Data.Streaming.Zlib as Z
@@ -79,7 +80,7 @@ gzip set app env sendResponse = app env $ \res ->
7980 case res of
8081 ResponseRaw{} -> sendResponse res
8182 ResponseFile{} | gzipFiles set == GzipIgnore -> sendResponse res
82− _ -> if "gzip" `elem` enc && not isMSIE6 && not (isEncoded res)
83+ _ -> if "gzip" `elem` enc && not isMSIE6 && not (isEncoded res) && (bigEnough res)
8384 then
8485 case (res, gzipFiles set) of
8586 (ResponseFile s hs file Nothing, GzipCacheFolder cache) ->
@@ -96,6 +97,16 @@ gzip set app env sendResponse = app env $ \res ->
9697 isMSIE6 = "MSIE 6" `S.isInfixOf` ua
9798 isEncoded res = isJust $ lookup hContentEncoding $ responseHeaders res
9899
100+ bigEnough rsp = case contentLength (responseHeaders rsp) of
101+ Nothing -> True -- This could be a streaming case
102+ Just len -> len >= minimumLength
103+
104+ -- For a small enough response, gzipping will actually increase the size
105+ -- Potentially for anything less than 860 bytes gzipping could be a net loss
106+ -- The actual number is application specific though and may need to be adjusted
107+ -- http://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits
108+ minimumLength = 860
109+
99110compressFile :: Status -> [Header] -> FilePath -> FilePath -> (Response -> IO a) -> IO a
100111compressFile s hs file cache sendResponse = do
101112 e <- doesFileExist tmpfile