From bb44a7d7789a518c291c194d697b80e90b9f3a31 Mon Sep 17 00:00:00 2001 From: Justin Ribeiro Date: Mon, 13 Aug 2018 22:48:21 -0700 Subject: [PATCH] core(uses-long-cache-ttl): handle multiple cache-control headers (#5745) --- .../byte-efficiency/uses-long-cache-ttl.js | 8 +++++++- .../uses-long-cache-ttl-test.js | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lighthouse-core/audits/byte-efficiency/uses-long-cache-ttl.js b/lighthouse-core/audits/byte-efficiency/uses-long-cache-ttl.js index 32462e039c24..bbea54ac7c17 100644 --- a/lighthouse-core/audits/byte-efficiency/uses-long-cache-ttl.js +++ b/lighthouse-core/audits/byte-efficiency/uses-long-cache-ttl.js @@ -185,7 +185,13 @@ class CacheHeaders extends Audit { /** @type {Map} */ const headers = new Map(); for (const header of record.responseHeaders || []) { - headers.set(header.name.toLowerCase(), header.value); + if (headers.has(header.name.toLowerCase())) { + const previousHeaderValue = headers.get(header.name.toLowerCase()); + headers.set(header.name.toLowerCase(), + `${previousHeaderValue}, ${header.value}`); + } else { + headers.set(header.name.toLowerCase(), header.value); + } } const cacheControl = parseCacheControl(headers.get('cache-control')); diff --git a/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js b/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js index 148fa6df213f..d49bae881c1b 100644 --- a/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js +++ b/lighthouse-core/test/audits/byte-efficiency/uses-long-cache-ttl-test.js @@ -119,6 +119,25 @@ describe('Cache headers audit', () => { }); }); + it('respects multiple cache-control headers', () => { + networkRecords = [ + networkRecord({headers: { + 'cache-control': 'max-age=31536000, public', + 'Cache-control': 'no-transform', + }}), + networkRecord({headers: { + 'Cache-Control': 'no-transform', + 'cache-control': 'max-age=3600', + 'Cache-control': 'public', + }}), + ]; + + return CacheHeadersAudit.audit(artifacts, {options}).then(result => { + const items = result.extendedInfo.value.results; + assert.equal(items.length, 1); + }); + }); + it('catches records with Etags', () => { networkRecords = [ networkRecord({headers: {etag: 'md5hashhere'}}),