Skip to content

Commit 4f52d4e

Browse files
atcastledylhunn
authored andcommittedOct 19, 2022
fix(common): don't generate srcset if noopImageLoader is used (#47804)
Do not generate a srcset if the loader being used is the default noopImageLoader. This loader does not take width into account, so it does not make sense to use it with srcsets. PR Close #47804
1 parent 716352c commit 4f52d4e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed
 

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {
418418

419419
if (this.ngSrcset) {
420420
rewrittenSrcset = this.getRewrittenSrcset();
421-
} else if (!this._disableOptimizedSrcset && !this.srcset) {
421+
} else if (
422+
!this._disableOptimizedSrcset && !this.srcset && this.imageLoader !== noopImageLoader) {
422423
rewrittenSrcset = this.getAutomaticSrcset();
423424
}
424425

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,20 @@ describe('Image directive', () => {
14011401
return `${IMG_BASE_URL}/${config.src}${width}`;
14021402
};
14031403

1404+
it('should not generate a srcset if the default noop loader is used', () => {
1405+
setupTestingModule({noLoader: true});
1406+
1407+
const template = `
1408+
<img ngSrc="img" width="100" height="50" sizes="100vw">
1409+
`;
1410+
const fixture = createTestComponent(template);
1411+
fixture.detectChanges();
1412+
1413+
const nativeElement = fixture.nativeElement as HTMLElement;
1414+
const img = nativeElement.querySelector('img')!;
1415+
expect(img.getAttribute('srcset')).toBeNull();
1416+
});
1417+
14041418
it('should add a responsive srcset to the img element if sizes attribute exists', () => {
14051419
setupTestingModule({imageLoader});
14061420

@@ -1538,6 +1552,7 @@ class TestComponent {
15381552
function setupTestingModule(config?: {
15391553
imageConfig?: ImageConfig,
15401554
imageLoader?: ImageLoader,
1555+
noLoader?: boolean,
15411556
extraProviders?: Provider[],
15421557
component?: Type<unknown>
15431558
}) {
@@ -1548,8 +1563,8 @@ function setupTestingModule(config?: {
15481563
const loader = config?.imageLoader || defaultLoader;
15491564
const extraProviders = config?.extraProviders || [];
15501565
const providers: Provider[] = [
1551-
{provide: DOCUMENT, useValue: window.document}, {provide: IMAGE_LOADER, useValue: loader},
1552-
...extraProviders
1566+
{provide: DOCUMENT, useValue: window.document},
1567+
...(config?.noLoader ? [] : [{provide: IMAGE_LOADER, useValue: loader}]), ...extraProviders
15531568
];
15541569
if (config?.imageConfig) {
15551570
providers.push({provide: IMAGE_CONFIG, useValue: config.imageConfig});

0 commit comments

Comments
 (0)
Please sign in to comment.