Skip to content

Commit

Permalink
#414986 > improvements and lcp by metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeSimoes committed Dec 29, 2023
1 parent 893c745 commit 1d6c8de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion blocks/card/card.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import ComponentBase from '../../scripts/component-base.js';
import { eagerImage } from '../../scripts/libs.js';

export default class Card extends ComponentBase {
static get observedAttributes() {
return ['columns', 'ratio'];
return ['columns', 'ratio', 'eager'];
}

connected() {
this.eager = parseInt(this.getAttribute('eager') || 0, 10);
this.setupColumns(this.getAttribute('columns'));
if (this.eager) {
eagerImage(this, this.eager);
}
}

setupColumns(columns) {
Expand Down
7 changes: 7 additions & 0 deletions scripts/libs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ export const debounce = (func, wait, immediate) => {
}
};
};

export const eagerImage = (block, length = 1) => {
const imgs = Array.from(block.querySelectorAll('img')).slice(0, length);
imgs.forEach((img) => {
img.setAttribute('loading', 'eager');
});
};

0 comments on commit 1d6c8de

Please sign in to comment.