Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(image-usage): use min of resourceSize/transferSize #4968

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ class OptimizedImages extends Gatherer {
const isSameOrigin = URL.originsMatch(pageUrl, record._url);
const isBase64DataUri = /^data:.{2,40}base64\s*,/.test(record._url);

if (isOptimizableImage && record._resourceSize > MINIMUM_IMAGE_SIZE) {
const actualResourceSize = Math.min(record._resourceSize, record._transferSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can _transferSize be larger than _resourceSize? Seems possible, especially with image data, but even in that case wouldn't we still want to use the transfer size because they're sending too many bytes (it's just in that case one of the culprits is a bad choice to gzip?)

Copy link
Collaborator Author

@patrickhulce patrickhulce Apr 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in every case I've seen with my own two 👀 for images transferSize = resourceSize + size of headers, highly irregular to try to gzip/brotli/etc an image since images are always compressed with their own domain-specific method that is way more effective, but apparently the tweet ran into that case with a 30MB image 😆

if (isOptimizableImage && actualResourceSize > MINIMUM_IMAGE_SIZE) {
prev.push({
isSameOrigin,
isBase64DataUri,
requestId: record._requestId,
url: record._url,
mimeType: record._mimeType,
resourceSize: record._resourceSize,
resourceSize: actualResourceSize,
});
}

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/gather/gatherers/image-usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ImageUsage extends Gatherer {
if (/^image/.test(record._mimeType) && record.finished) {
map[record._url] = {
url: record.url,
resourceSize: record.resourceSize,
resourceSize: Math.min(record.resourceSize, record.transferSize),
startTime: record.startTime,
endTime: record.endTime,
responseReceivedTime: record.responseReceivedTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,47 @@ const traceData = {
_url: 'http://google.com/image.jpg',
_mimeType: 'image/jpeg',
_resourceSize: 10000,
_transferSize: 20000,
_resourceType: {_name: 'image'},
finished: true,
},
{
_url: 'http://google.com/transparent.png',
_mimeType: 'image/png',
_resourceSize: 11000,
_transferSize: 20000,
_resourceType: {_name: 'image'},
finished: true,
},
{
_url: 'http://google.com/image.bmp',
_mimeType: 'image/bmp',
_resourceSize: 12000,
_transferSize: 9000, // bitmap was compressed another way
_resourceType: {_name: 'image'},
finished: true,
},
{
_url: 'http://google.com/image.bmp',
_mimeType: 'image/bmp',
_resourceSize: 12000,
_transferSize: 20000,
_resourceType: {_name: 'image'},
finished: true,
},
{
_url: 'http://google.com/vector.svg',
_mimeType: 'image/svg+xml',
_resourceSize: 13000,
_transferSize: 20000,
_resourceType: {_name: 'image'},
finished: true,
},
{
_url: 'http://gmail.com/image.jpg',
_mimeType: 'image/jpeg',
_resourceSize: 15000,
_transferSize: 20000,
_resourceType: {_name: 'image'},
finished: true,
},
Expand All @@ -66,20 +72,23 @@ const traceData = {
_mimeType: 'image/jpeg',
_resourceType: {_name: 'image'},
_resourceSize: 14000,
_transferSize: 20000,
finished: true,
},
{
_url: 'http://google.com/big-image.bmp',
_mimeType: 'image/bmp',
_resourceType: {_name: 'image'},
_resourceSize: 12000,
_transferSize: 20000,
finished: false, // ignore for not finishing
},
{
_url: 'http://google.com/not-an-image.bmp',
_mimeType: 'image/bmp',
_resourceType: {_name: 'document'}, // ignore for not really being an image
_resourceSize: 12000,
_transferSize: 20000,
finished: true,
},
],
Expand Down Expand Up @@ -125,7 +134,7 @@ describe('Optimized images', () => {
assert.equal(artifact.length, 4);
checkSizes(artifact[0], 10000, 60, 80);
checkSizes(artifact[1], 11000, 60, 80);
checkSizes(artifact[2], 12000, 60, 80);
checkSizes(artifact[2], 9000, 60, 80);
// skip cross-origin for now
// checkSizes(artifact[3], 15000, 60, 80);
checkSizes(artifact[3], 20, 80, 100); // uses base64 data
Expand Down