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(modern-image-formats): ignore images CF claims are bigger as webp #14358

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 12 additions & 3 deletions core/audits/byte-efficiency/modern-image-formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/*

/**
* @fileoverview This audit determines if the images could be smaller when compressed with WebP.
*/


import {ByteEfficiencyAudit} from './byte-efficiency-audit.js';
import URL from '../../lib/url-shim.js';
import * as i18n from '../../lib/i18n/i18n.js';
Expand Down Expand Up @@ -89,12 +89,14 @@ class ModernImageFormats extends ByteEfficiencyAudit {

/**
* @param {LH.Artifacts} artifacts
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
* @return {import('./byte-efficiency-audit.js').ByteEfficiencyProduct}
*/
static audit_(artifacts) {
static audit_(artifacts, networkRecords) {
const pageURL = artifacts.URL.finalUrl;
const images = artifacts.OptimizedImages;
const imageElements = artifacts.ImageElements;

/** @type {Map<string, LH.Artifacts.ImageElement>} */
const imageElementsByURL = new Map();
imageElements.forEach(img => imageElementsByURL.set(img.src, img));
Expand All @@ -113,6 +115,13 @@ class ModernImageFormats extends ByteEfficiencyAudit {
// Skip if the image was already using a modern format.
if (image.mimeType === 'image/webp' || image.mimeType === 'image/avif') continue;

// Skip if Cloudflare's automatic conversion detected that converting wasn't worth it.
// https://support.cloudflare.com/hc/en-us/articles/4412244347917-Troubleshoot-common-Cf-Polished-statuses
const record = networkRecords.find(record => record.requestId === image.requestId);
const cfPolishedHeader =
record?.responseHeaders.find(h => h.name.toLowerCase() === 'cf-polish');
if (cfPolishedHeader?.value.includes('webp_bigger')) continue;

const jpegSize = image.jpegSize;
let webpSize = image.webpSize;
let avifSize = ModernImageFormats.estimateAvifSizeFromWebPAndJpegEstimates({
Expand Down