Skip to content

Commit 50f08e6

Browse files
atcastlealxhub
authored andcommitted
feat(common): automatically use sizes auto in NgOptimizedImage (#57479)
Prepend 'auto' to value of the `sizes` prop generated by NgOptimizedImage, when image has a responsive srcset and is lazy-loaded. PR Close #57479
1 parent 5a0ff41 commit 50f08e6

3 files changed

Lines changed: 86 additions & 4 deletions

File tree

‎adev/src/content/guide/image-optimization.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ If you haven't used `sizes` before, a good place to start is to set it based on
248248

249249
If you find that the above does not cover your desired image behavior, see the documentation on [advanced sizes values](#advanced-sizes-values).
250250

251+
Note that `NgOptimizedImage` automatically prepends `"auto"` to the provided `sizes` value. This is an optimization that increases the accuracy of srcset selection on browsers which support `sizes="auto"`, and is ignored by browsers which do not.
252+
251253
By default, the responsive breakpoints are:
252254

253255
`[16, 32, 48, 64, 96, 128, 256, 384, 640, 750, 828, 1080, 1200, 1920, 2048, 3840]`

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,21 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
492492
const rewrittenSrcset = this.updateSrcAndSrcset();
493493

494494
if (this.sizes) {
495-
this.setHostAttribute('sizes', this.sizes);
495+
if (this.getLoadingBehavior() === 'lazy') {
496+
this.setHostAttribute('sizes', 'auto, ' + this.sizes);
497+
} else {
498+
this.setHostAttribute('sizes', this.sizes);
499+
}
500+
} else {
501+
if (
502+
this.ngSrcset &&
503+
VALID_WIDTH_DESCRIPTOR_SRCSET.test(this.ngSrcset) &&
504+
this.getLoadingBehavior() === 'lazy'
505+
) {
506+
this.setHostAttribute('sizes', 'auto, 100vw');
507+
}
496508
}
509+
497510
if (this.isServer && this.priority) {
498511
this.preloadLinkCreator.createPreloadLinkTag(
499512
this.renderer,

‎packages/common/test/directives/ng_optimized_image_spec.ts‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,25 +1027,47 @@ describe('Image directive', () => {
10271027
it('should add default sizes value in fill mode', () => {
10281028
setupTestingModule();
10291029

1030-
const template = '<img ngSrc="path/img.png" fill>';
1030+
const template = '<img ngSrc="path/img.png" fill priority>';
10311031

10321032
const fixture = createTestComponent(template);
10331033
fixture.detectChanges();
10341034
const nativeElement = fixture.nativeElement as HTMLElement;
10351035
const img = nativeElement.querySelector('img')!;
10361036
expect(img.getAttribute('sizes')).toBe('100vw');
10371037
});
1038+
it('should add auto sizes to default in fill mode when lazy', () => {
1039+
setupTestingModule();
1040+
1041+
const template = '<img ngSrc="path/img.png" fill>';
1042+
1043+
const fixture = createTestComponent(template);
1044+
fixture.detectChanges();
1045+
const nativeElement = fixture.nativeElement as HTMLElement;
1046+
const img = nativeElement.querySelector('img')!;
1047+
expect(img.getAttribute('sizes')).toBe('auto, 100vw');
1048+
});
10381049
it('should not overwrite sizes value in fill mode', () => {
10391050
setupTestingModule();
10401051

1041-
const template = '<img ngSrc="path/img.png" sizes="50vw" fill>';
1052+
const template = '<img ngSrc="path/img.png" sizes="50vw" fill loading="eager">';
10421053

10431054
const fixture = createTestComponent(template);
10441055
fixture.detectChanges();
10451056
const nativeElement = fixture.nativeElement as HTMLElement;
10461057
const img = nativeElement.querySelector('img')!;
10471058
expect(img.getAttribute('sizes')).toBe('50vw');
10481059
});
1060+
it('should prepend "auto" to sizes in fill mode when lazy', () => {
1061+
setupTestingModule();
1062+
1063+
const template = '<img ngSrc="path/img.png" sizes="50vw" fill>';
1064+
1065+
const fixture = createTestComponent(template);
1066+
fixture.detectChanges();
1067+
const nativeElement = fixture.nativeElement as HTMLElement;
1068+
const img = nativeElement.querySelector('img')!;
1069+
expect(img.getAttribute('sizes')).toBe('auto, 50vw');
1070+
});
10491071
it('should cause responsive srcset to be generated in fill mode', () => {
10501072
setupTestingModule();
10511073

@@ -2022,6 +2044,34 @@ describe('Image directive', () => {
20222044
`${IMG_BASE_URL}/img.png?w=300 3x`,
20232045
);
20242046
});
2047+
it('should automatically set a default sizes attribute when ngSrcset is used with a responsive srcset and is lazy', () => {
2048+
setupTestingModule({imageLoader});
2049+
2050+
const template = `
2051+
<img ngSrc="img.png" ngSrcset="100w, 200w, 300w" width="100" height="50">
2052+
`;
2053+
const fixture = createTestComponent(template);
2054+
fixture.detectChanges();
2055+
2056+
const nativeElement = fixture.nativeElement as HTMLElement;
2057+
const img = nativeElement.querySelector('img')!;
2058+
expect(img.src).toBe(`${IMG_BASE_URL}/img.png`);
2059+
expect(img.sizes).toBe(`auto, 100vw`);
2060+
});
2061+
it('should not automatically set a default sizes attribute when ngSrcset is used with a responsive srcset and is not lazy', () => {
2062+
setupTestingModule({imageLoader});
2063+
2064+
const template = `
2065+
<img ngSrc="img.png" ngSrcset="100w, 200w, 300w" width="100" height="50" priority>
2066+
`;
2067+
const fixture = createTestComponent(template);
2068+
fixture.detectChanges();
2069+
2070+
const nativeElement = fixture.nativeElement as HTMLElement;
2071+
const img = nativeElement.querySelector('img')!;
2072+
expect(img.src).toBe(`${IMG_BASE_URL}/img.png`);
2073+
expect(img.sizes).toBe('');
2074+
});
20252075
});
20262076

20272077
describe('sizes attribute', () => {
@@ -2030,7 +2080,7 @@ describe('Image directive', () => {
20302080

20312081
const template =
20322082
'<img ngSrc="path/img.png" width="150" height="50" ' +
2033-
'sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw">';
2083+
'sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw" priority>';
20342084
const fixture = createTestComponent(template);
20352085
fixture.detectChanges();
20362086

@@ -2042,6 +2092,23 @@ describe('Image directive', () => {
20422092
);
20432093
});
20442094

2095+
it('should prepend sizes="auto" to a lazy-loaded image', () => {
2096+
setupTestingModule();
2097+
2098+
const template =
2099+
'<img ngSrc="path/img.png" width="150" height="50" ' +
2100+
'sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw">';
2101+
const fixture = createTestComponent(template);
2102+
fixture.detectChanges();
2103+
2104+
const nativeElement = fixture.nativeElement as HTMLElement;
2105+
const img = nativeElement.querySelector('img')!;
2106+
2107+
expect(img.getAttribute('sizes')).toBe(
2108+
'auto, (max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw',
2109+
);
2110+
});
2111+
20452112
it('should throw if a complex `sizes` is used', () => {
20462113
setupTestingModule();
20472114

0 commit comments

Comments
 (0)