Skip to content

Commit

Permalink
core(optimized-images): support non-standard mime types (#5688)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored and paulirish committed Jul 19, 2018
1 parent 8cee113 commit 1f52fe7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const WEBP_QUALITY = 0.85;

const MINIMUM_IMAGE_SIZE = 4096; // savings of <4 KB will be ignored in the audit anyway

const IMAGE_REGEX = /^image\/((x|ms|x-ms)-)?(png|bmp|jpeg)$/;

/** @typedef {{isSameOrigin: boolean, isBase64DataUri: boolean, requestId: string, url: string, mimeType: string, resourceSize: number}} SimplifiedNetworkRecord */

/* global document, Image, atob */
Expand Down Expand Up @@ -87,7 +89,7 @@ class OptimizedImages extends Gatherer {

seenUrls.add(record.url);
const isOptimizableImage = record.resourceType === NetworkRequest.TYPES.Image &&
/image\/(png|bmp|jpeg)/.test(record.mimeType);
IMAGE_REGEX.test(record.mimeType);
const isSameOrigin = URL.originsMatch(pageUrl, record.url);
const isBase64DataUri = /^data:.{2,40}base64\s*,/.test(record.url);

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/dobetterweb/doctype-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const Audit = require('../../../audits/dobetterweb/doctype.js');
const assert = require('assert');

/* eslint-env mocha */
/* eslint-env jest */

describe('DOBETTERWEB: doctype audit', () => {
it('fails when document does not contain a doctype', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
'use strict';

/* eslint-env mocha */
/* eslint-env jest */

const OptimizedImages =
require('../../../../gather/gatherers/dobetterweb/optimized-images');
Expand Down Expand Up @@ -185,4 +185,23 @@ describe('Optimized images', () => {
assert.ok(/gmail.*image.jpg/.test(artifact[3].url));
});
});

it('handles non-standard mime types too', async () => {
const traceData = {
networkRecords: [
{
requestId: '1',
url: 'http://google.com/image.bmp?x-ms',
mimeType: 'image/x-ms-bmp',
resourceSize: 12000,
transferSize: 20000,
resourceType: 'Image',
finished: true,
},
],
};

const artifact = await optimizedImages.afterPass(options, traceData);
expect(artifact).toHaveLength(1);
});
});

0 comments on commit 1f52fe7

Please sign in to comment.