Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): don't generate srcset if noopImageLoader is used #47804

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -417,7 +417,8 @@ export class NgOptimizedImage implements OnInit, OnChanges, OnDestroy {

if (this.ngSrcset) {
rewrittenSrcset = this.getRewrittenSrcset();
} else if (!this._disableOptimizedSrcset && !this.srcset) {
} else if (
!this._disableOptimizedSrcset && !this.srcset && this.imageLoader !== noopImageLoader) {
rewrittenSrcset = this.getAutomaticSrcset();
}

Expand Down
19 changes: 17 additions & 2 deletions packages/common/test/directives/ng_optimized_image_spec.ts
Expand Up @@ -1401,6 +1401,20 @@ describe('Image directive', () => {
return `${IMG_BASE_URL}/${config.src}${width}`;
};

it('should not generate a srcset if the default noop loader is used', () => {
setupTestingModule({noLoader: true});

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

const nativeElement = fixture.nativeElement as HTMLElement;
const img = nativeElement.querySelector('img')!;
expect(img.getAttribute('srcset')).toBeNull();
});

it('should add a responsive srcset to the img element if sizes attribute exists', () => {
setupTestingModule({imageLoader});

Expand Down Expand Up @@ -1538,6 +1552,7 @@ class TestComponent {
function setupTestingModule(config?: {
imageConfig?: ImageConfig,
imageLoader?: ImageLoader,
noLoader?: boolean,
extraProviders?: Provider[],
component?: Type<unknown>
}) {
Expand All @@ -1548,8 +1563,8 @@ function setupTestingModule(config?: {
const loader = config?.imageLoader || defaultLoader;
const extraProviders = config?.extraProviders || [];
const providers: Provider[] = [
{provide: DOCUMENT, useValue: window.document}, {provide: IMAGE_LOADER, useValue: loader},
...extraProviders
{provide: DOCUMENT, useValue: window.document},
...(config?.noLoader ? [] : [{provide: IMAGE_LOADER, useValue: loader}]), ...extraProviders
];
if (config?.imageConfig) {
providers.push({provide: IMAGE_CONFIG, useValue: config.imageConfig});
Expand Down