Skip to content

Commit 8f2b0ed

Browse files
arturovtalxhub
authored andcommitted
fix(common): skip checking whether SVGs are oversized (#57966)
Prior to this commit, the `ImagePerformanceWarning` class was checking all `img` elements in the DOM to determine whether they were oversized after the DOM loading was complete. We should not check SVGs because they are vector-based and can scale infinitely without losing quality. Closes #57941 PR Close #57966
1 parent add5c25 commit 8f2b0ed

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

packages/core/src/image_performance_warning.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ export class ImagePerformanceWarning implements OnDestroy {
140140
if (!this.window) {
141141
return false;
142142
}
143+
144+
// The `isOversized` check may not be applicable or may require adjustments
145+
// for several types of image formats or scenarios. Currently, we specify only
146+
// `svg`, but this may also include `gif` since their quality isn’t tied to
147+
// dimensions in the same way as raster images.
148+
const nonOversizedImageExtentions = [
149+
// SVG images are vector-based, which means they can scale
150+
// to any size without losing quality.
151+
'.svg',
152+
];
153+
154+
// Convert it to lowercase because this may have uppercase
155+
// extensions, such as `IMAGE.SVG`.
156+
// We fallback to an empty string because `src` may be `undefined`
157+
// if it is explicitly set to `null` by some third-party code
158+
// (e.g., `image.src = null`).
159+
const imageSource = (image.src || '').toLowerCase();
160+
161+
if (nonOversizedImageExtentions.some((extension) => imageSource.endsWith(extension))) {
162+
return false;
163+
}
164+
143165
const computedStyle = this.window.getComputedStyle(image);
144166
let renderedWidth = parseFloat(computedStyle.getPropertyValue('width'));
145167
let renderedHeight = parseFloat(computedStyle.getPropertyValue('height'));
@@ -153,6 +175,8 @@ export class ImagePerformanceWarning implements OnDestroy {
153175
}
154176

155177
if (boxSizing === 'border-box') {
178+
// If the image `box-sizing` is set to `border-box`, we adjust the rendered
179+
// dimensions by subtracting padding values.
156180
const paddingTop = computedStyle.getPropertyValue('padding-top');
157181
const paddingRight = computedStyle.getPropertyValue('padding-right');
158182
const paddingBottom = computedStyle.getPropertyValue('padding-bottom');

packages/core/test/bundling/image-directive/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ng_module(
1010
"e2e/image-distortion/image-distortion.ts",
1111
"e2e/image-perf-warnings-lazy/image-perf-warnings-lazy.ts",
1212
"e2e/image-perf-warnings-oversized/image-perf-warnings-oversized.ts",
13+
"e2e/image-perf-warnings-oversized/svg-no-perf-oversized-warnings.ts",
1314
"e2e/lcp-check/lcp-check.ts",
1415
"e2e/oversized-image/oversized-image.ts",
1516
"e2e/preconnect-check/preconnect-check.ts",

packages/core/test/bundling/image-directive/e2e/image-perf-warnings-oversized/image-perf-warnings-oversized.e2e-spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,16 @@ describe('Image performance warnings', () => {
2424
const expectedMessageRegex = /has intrinsic file dimensions much larger than/;
2525
expect(expectedMessageRegex.test(logs[0].message)).toBeTruthy();
2626
});
27+
28+
// https://github.com/angular/angular/issues/57941
29+
it('should NOT warn if rendered SVG image size is much smaller that intrinsic size', async () => {
30+
await browser.get('/e2e/svg-no-perf-oversized-warnings');
31+
// Wait for load event
32+
await new Promise((resolve) => setTimeout(resolve, 600));
33+
34+
const logs = await collectBrowserLogs(logging.Level.WARNING);
35+
// Please note that prior to shipping the fix, it was logging a warning
36+
// for the SVG image (see the attached issue above).
37+
expect(logs.length).toEqual(0);
38+
});
2739
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {Component} from '@angular/core';
10+
11+
@Component({
12+
selector: 'svg-no-perf-oversized-warnings',
13+
standalone: true,
14+
template: `
15+
<!-- Image is rendered too small -->
16+
<div style="width: 200px; height: 200px">
17+
<img src="/e2e/logo-1500w.svg" width="100" height="100">
18+
</div>
19+
`,
20+
})
21+
export class SvgNoOversizedPerfWarningsComponent {}

0 commit comments

Comments
 (0)