Skip to content

Commit

Permalink
fix(common): apply fixed_srcset_width value only to fixed srcsets (#5…
Browse files Browse the repository at this point in the history
…2459)

add logic to NgOptimizedImage to keep fixed_srcset_width from applying to large responsive images, which is incorrect behavior

PR Close #52459
  • Loading branch information
atcastle authored and alxhub committed Oct 31, 2023
1 parent af46256 commit 3bd85fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Expand Up @@ -513,8 +513,13 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
}

private shouldGenerateAutomaticSrcset(): boolean {
let oversizedImage = false;
if (!this.sizes) {
oversizedImage =
this.width! > FIXED_SRCSET_WIDTH_LIMIT || this.height! > FIXED_SRCSET_HEIGHT_LIMIT;
}
return !this.disableOptimizedSrcset && !this.srcset && this.imageLoader !== noopImageLoader &&
!(this.width! > FIXED_SRCSET_WIDTH_LIMIT || this.height! > FIXED_SRCSET_HEIGHT_LIMIT);
!oversizedImage;
}

/** @nodoc */
Expand Down
32 changes: 32 additions & 0 deletions packages/common/test/directives/ng_optimized_image_spec.ts
Expand Up @@ -1797,6 +1797,38 @@ describe('Image directive', () => {
expect(img.getAttribute('srcset')).toBeNull();
});

it('should add a responsive srcset to the img element if height is too large', () => {
setupTestingModule({imageLoader});

const template = `<img ngSrc="img" width="1100" height="2400" sizes="100vw">`;
const fixture = createTestComponent(template);
fixture.detectChanges();

const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.getAttribute('srcset'))
.toBe(`${IMG_BASE_URL}/img?w=640 640w, ${IMG_BASE_URL}/img?w=750 750w, ${
IMG_BASE_URL}/img?w=828 828w, ${IMG_BASE_URL}/img?w=1080 1080w, ${
IMG_BASE_URL}/img?w=1200 1200w, ${IMG_BASE_URL}/img?w=1920 1920w, ${
IMG_BASE_URL}/img?w=2048 2048w, ${IMG_BASE_URL}/img?w=3840 3840w`);
});

it('should add a responsive srcset to the img element if width is too large', () => {
setupTestingModule({imageLoader});

const template = `<img ngSrc="img" width="3000" height="400" sizes="100vw">`;
const fixture = createTestComponent(template);
fixture.detectChanges();

const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.getAttribute('srcset'))
.toBe(`${IMG_BASE_URL}/img?w=640 640w, ${IMG_BASE_URL}/img?w=750 750w, ${
IMG_BASE_URL}/img?w=828 828w, ${IMG_BASE_URL}/img?w=1080 1080w, ${
IMG_BASE_URL}/img?w=1200 1200w, ${IMG_BASE_URL}/img?w=1920 1920w, ${
IMG_BASE_URL}/img?w=2048 2048w, ${IMG_BASE_URL}/img?w=3840 3840w`);
});

it('should use a custom breakpoint set if one is provided', () => {
const imageConfig = {
breakpoints: [16, 32, 48, 64, 96, 128, 256, 384, 640, 1280, 3840],
Expand Down

0 comments on commit 3bd85fb

Please sign in to comment.