minify: Fix gzip not triggered for packages (#4491)

* caching_middleware: fix gzip compression not triggered

* packages: If a client sets `Accept-Encoding: gzip`, the responseCache will
include `Content-Encoding: gzip` in all future responses, even
if a subsequent request does not set `Accept-Encoding` or another client
requests the file without setting `Accept-Encoding`.
Fix that.

* caching_middleware: use `test` instead of `match`

* add tests

* make code easier to understand

* make the regex more clear
This commit is contained in:
webzwo0i 2020-11-22 10:23:33 +01:00 committed by GitHub
parent 1d491c0059
commit f2febcfc7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 171 additions and 3 deletions

View file

@ -20,7 +20,6 @@ var fs = require('fs');
var path = require('path');
var zlib = require('zlib');
var settings = require('./Settings');
var semver = require('semver');
var existsSync = require('./path_exists');
/*
@ -192,11 +191,12 @@ CachingMiddleware.prototype = new function () {
res.write = old_res.write || res.write;
res.end = old_res.end || res.end;
var headers = responseCache[cacheKey].headers;
let headers = {};
Object.assign(headers, (responseCache[cacheKey].headers || {}));
var statusCode = responseCache[cacheKey].statusCode;
var pathStr = CACHE_DIR + 'minified_' + cacheKey;
if (supportsGzip && (headers['content-type'] || '').match(/^text\//)) {
if (supportsGzip && /application\/javascript/.test(headers['content-type'])) {
pathStr = pathStr + '.gz';
headers['content-encoding'] = 'gzip';
}