Skip to content

Commit 07400fd

Browse files
committed
fix(Imageloader): fixed issue where browser would load both 1x and 2x images if the element was invi
1 parent 1d1e9c3 commit 07400fd

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/app/image-loader/image-loader.component.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ describe('ImageLoaderComponent', () => {
8787

8888
it('should set supportsSrcSet value', () => {
8989
component.supportsSrcSet = false;
90+
component.inViewport = true;
91+
component.loaded = false;
9092
const img = document.createElement('img');
9193
let srcSupport = false;
9294
if ('srcset' in img) {
9395
srcSupport = true;
9496
}
95-
component.ngAfterViewInit();
97+
component.preloadImage();
9698
expect(component.supportsSrcSet).toEqual(srcSupport);
9799
});
98100

@@ -143,6 +145,7 @@ describe('ImageLoaderComponent', () => {
143145
component.supportsSrcSet = false;
144146
component.size = 'xs';
145147
component.loaded = false;
148+
component.dummyImg.nativeElement = { src: null };
146149
component.preloadImage();
147150
expect(component.preloadSrc).toEqual('http://via.placeholder.com/150x350?text=xs+1x');
148151

@@ -157,6 +160,7 @@ describe('ImageLoaderComponent', () => {
157160
component.supportsSrcSet = false;
158161
component.size = 'xs';
159162
component.loaded = false;
163+
component.dummyImg.nativeElement = { src: null };
160164
component.preloadImage();
161165
expect(component.preloadSrc).toEqual('http://via.placeholder.com/150x350?text=xs+1x');
162166

src/app/image-loader/image-loader.component.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ export class ImageLoaderComponent implements OnInit, AfterViewInit, OnDestroy {
208208
* @memberof ImageLoaderComponent
209209
*/
210210
public ngAfterViewInit(): void {
211-
const dummyImg: HTMLElement = this.dummyImg.nativeElement;
212-
if ('srcset' in dummyImg) {
213-
this.supportsSrcSet = true;
214-
}
215211
this.width$
216212
.takeUntil(this.ngUnsubscribe$)
217213
.debounceTime(10)
@@ -275,6 +271,9 @@ export class ImageLoaderComponent implements OnInit, AfterViewInit, OnDestroy {
275271
const retinaImg = this.image.images.filter(retinaImage => retinaImage.size === this.size)[0];
276272
const imageNormal = retinaImg.x1;
277273
const imageRetina = retinaImg.x2;
274+
if ('srcset' in this.dummyImg.nativeElement) {
275+
this.supportsSrcSet = true;
276+
}
278277
this.supportsSrcSet ?
279278
this.preloadSrcset = `${imageNormal} 1x, ${imageRetina} 2x` :
280279
this.preloadSrc = `${imageNormal}`;

0 commit comments

Comments
 (0)