From 1d6c8dea4b567973ab65dc4cd5811b0082e2cb68 Mon Sep 17 00:00:00 2001 From: Felipe Simoes Date: Fri, 29 Dec 2023 11:12:15 +0100 Subject: [PATCH] #414986 > improvements and lcp by metadata --- blocks/card/card.js | 7 ++++++- scripts/libs.js | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/blocks/card/card.js b/blocks/card/card.js index ebcff782..cd9b1026 100644 --- a/blocks/card/card.js +++ b/blocks/card/card.js @@ -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) { diff --git a/scripts/libs.js b/scripts/libs.js index 2811bd1d..8ee02448 100644 --- a/scripts/libs.js +++ b/scripts/libs.js @@ -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'); + }); +};