Skip to content

Commit

Permalink
Support for above the fold data tag on iframes too
Browse files Browse the repository at this point in the history
  • Loading branch information
georges-gomes committed Dec 5, 2024
1 parent 06db1f5 commit f6349f5
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function analyse(state: GlobalState, file: string): Promise<void> {
);

const img = $(imgElement);
const isAboveTheFold = isImgAboveTheFold(img, imgElement, theFold);
const isAboveTheFold = isElementAboveTheFold(img, imgElement, theFold);
img.removeAttr(ABOVE_FOLD_DATA_ATTR);

try {
Expand Down Expand Up @@ -104,7 +104,8 @@ async function analyse(state: GlobalState, file: string): Promise<void> {
`<iframe> [${i + 1}/${iframesArray.length}] ${$(ifElement).attr('src')}`
);

const isAboveTheFold = ifElement.startIndex! < theFold;
const ifr = $(ifElement);
const isAboveTheFold = isElementAboveTheFold(ifr, ifElement, theFold);

try {
await processIframe(file, $, ifElement, isAboveTheFold);
Expand Down Expand Up @@ -208,18 +209,18 @@ async function analyse(state: GlobalState, file: string): Promise<void> {
}
}

function isImgAboveTheFold(
img: cheerio.Cheerio<cheerio.Element>,
imgElement: cheerio.Element,
function isElementAboveTheFold(
cheerio: cheerio.Cheerio<cheerio.Element>,
element: cheerio.Element,
theFold: number
) {
const aboveTheFoldAttr: string | number | undefined =
img.attr(ABOVE_FOLD_DATA_ATTR);
cheerio.attr(ABOVE_FOLD_DATA_ATTR);
if (aboveTheFoldAttr) {
const parsed = parseInt(aboveTheFoldAttr);
if (!Number.isNaN(parsed)) return !!parsed;
}
return imgElement.startIndex! < theFold;
return element.startIndex! < theFold;
}

function getTheFold($: cheerio.CheerioAPI): number {
Expand Down

0 comments on commit f6349f5

Please sign in to comment.