Skip to content

Commit

Permalink
fix: Fix image lazy loading for Chrome in SSR mode (#18761)
Browse files Browse the repository at this point in the history
This PR provides the work around for not working image lazy loading in SSR in Chrome and Safari.

Closes: https://jira.tools.sap/browse/CXSPA-6719
  • Loading branch information
rmch91 authored Apr 25, 2024
1 parent 0b7d289 commit 064d237
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions projects/storefrontlib/shared/components/media/media.component.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
<picture *ngIf="media?.srcset && !isLegacy; else tmpImg">
<source
*ngFor="
let source of media!.srcset! | cxMediaSources;
trackBy: trackByMedia
"
[srcset]="source.srcset"
[media]="source.media"
/>
<ng-container *ngIf="!!media">
<picture *ngIf="media.srcset && !isLegacy; else legacyImgTmp">
<source
*ngFor="
let source of media!.srcset! | cxMediaSources;
trackBy: trackByMedia
"
[srcset]="source.srcset"
[media]="source.media"
/>

<ng-container [ngTemplateOutlet]="tmpImg"></ng-container>
</picture>
<img
[loading]="loading"
[alt]="media.alt"
[title]="media.alt"
[src]="media.src"
[attr.role]="media.role"
(load)="loadHandler()"
(error)="errorHandler()"
/>
</picture>

<ng-template #tmpImg>
<img
*ngIf="media && media.src"
[alt]="media.alt"
[title]="media.alt"
[src]="media.src"
[srcset]="media.srcset || media.src"
[attr.role]="media.role"
[loading]="loading"
(load)="loadHandler()"
(error)="errorHandler()"
/>
</ng-template>
<ng-template #legacyImgTmp>
<img
*ngIf="media.src"
[loading]="loading"
[alt]="media.alt"
[title]="media.alt"
[src]="media.src"
[srcset]="media?.srcset || media.src"
[attr.role]="media.role"
(load)="loadHandler()"
(error)="errorHandler()"
/>
</ng-template>
</ng-container>

0 comments on commit 064d237

Please sign in to comment.