Skip to content

Commit 3bd85fb

Browse files
atcastlealxhub
authored andcommitted
fix(common): apply fixed_srcset_width value only to fixed srcsets (#52459)
add logic to NgOptimizedImage to keep fixed_srcset_width from applying to large responsive images, which is incorrect behavior PR Close #52459
1 parent af46256 commit 3bd85fb

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

packages/common/src/directives/ng_optimized_image/ng_optimized_image.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,13 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
513513
}
514514

515515
private shouldGenerateAutomaticSrcset(): boolean {
516+
let oversizedImage = false;
517+
if (!this.sizes) {
518+
oversizedImage =
519+
this.width! > FIXED_SRCSET_WIDTH_LIMIT || this.height! > FIXED_SRCSET_HEIGHT_LIMIT;
520+
}
516521
return !this.disableOptimizedSrcset && !this.srcset && this.imageLoader !== noopImageLoader &&
517-
!(this.width! > FIXED_SRCSET_WIDTH_LIMIT || this.height! > FIXED_SRCSET_HEIGHT_LIMIT);
522+
!oversizedImage;
518523
}
519524

520525
/** @nodoc */

packages/common/test/directives/ng_optimized_image_spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,38 @@ describe('Image directive', () => {
17971797
expect(img.getAttribute('srcset')).toBeNull();
17981798
});
17991799

1800+
it('should add a responsive srcset to the img element if height is too large', () => {
1801+
setupTestingModule({imageLoader});
1802+
1803+
const template = `<img ngSrc="img" width="1100" height="2400" sizes="100vw">`;
1804+
const fixture = createTestComponent(template);
1805+
fixture.detectChanges();
1806+
1807+
const nativeElement = fixture.nativeElement as HTMLElement;
1808+
const img = nativeElement.querySelector('img')!;
1809+
expect(img.getAttribute('srcset'))
1810+
.toBe(`${IMG_BASE_URL}/img?w=640 640w, ${IMG_BASE_URL}/img?w=750 750w, ${
1811+
IMG_BASE_URL}/img?w=828 828w, ${IMG_BASE_URL}/img?w=1080 1080w, ${
1812+
IMG_BASE_URL}/img?w=1200 1200w, ${IMG_BASE_URL}/img?w=1920 1920w, ${
1813+
IMG_BASE_URL}/img?w=2048 2048w, ${IMG_BASE_URL}/img?w=3840 3840w`);
1814+
});
1815+
1816+
it('should add a responsive srcset to the img element if width is too large', () => {
1817+
setupTestingModule({imageLoader});
1818+
1819+
const template = `<img ngSrc="img" width="3000" height="400" sizes="100vw">`;
1820+
const fixture = createTestComponent(template);
1821+
fixture.detectChanges();
1822+
1823+
const nativeElement = fixture.nativeElement as HTMLElement;
1824+
const img = nativeElement.querySelector('img')!;
1825+
expect(img.getAttribute('srcset'))
1826+
.toBe(`${IMG_BASE_URL}/img?w=640 640w, ${IMG_BASE_URL}/img?w=750 750w, ${
1827+
IMG_BASE_URL}/img?w=828 828w, ${IMG_BASE_URL}/img?w=1080 1080w, ${
1828+
IMG_BASE_URL}/img?w=1200 1200w, ${IMG_BASE_URL}/img?w=1920 1920w, ${
1829+
IMG_BASE_URL}/img?w=2048 2048w, ${IMG_BASE_URL}/img?w=3840 3840w`);
1830+
});
1831+
18001832
it('should use a custom breakpoint set if one is provided', () => {
18011833
const imageConfig = {
18021834
breakpoints: [16, 32, 48, 64, 96, 128, 256, 384, 640, 1280, 3840],

0 commit comments

Comments
 (0)