From 834d8d01902ba313b9e468e130425c4e696d4571 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Wed, 28 May 2025 11:52:50 +0100 Subject: [PATCH] Add font family to allFonts object and push new variation only if matching url is valid --- src/BeaconPreloadFonts.js | 71 ++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/src/BeaconPreloadFonts.js b/src/BeaconPreloadFonts.js index 67cf22e..238234b 100644 --- a/src/BeaconPreloadFonts.js +++ b/src/BeaconPreloadFonts.js @@ -506,22 +506,6 @@ class BeaconPreloadFonts { const aboveElements = elements.filter(el => this.isElementAboveFold(el)); const belowElements = elements.filter(el => !this.isElementAboveFold(el)); - if (!allFonts[fontFamily]) { - allFonts[fontFamily] = { - type: 'hosted', - variations: [], - elementCount: { - aboveFold: aboveElements.length, - belowFold: belowElements.length, - total: elements.length - }, - urlCount: { - aboveFold: new Set(), - belowFold: new Set() - } - }; - } - data.variations.forEach(variation => { let matchingUrl = null; for (const styleUrl of data.urls) { @@ -532,20 +516,37 @@ class BeaconPreloadFonts { } } - // Add variation with correct element counts - allFonts[fontFamily].variations.push({ - weight: variation.weight, - style: variation.style, - url: matchingUrl || 'File not found', - elementCount: { - aboveFold: aboveElements.length, - belowFold: belowElements.length, - total: elements.length - } - }); - // Track URLs per location if (matchingUrl) { + // Only create new object if a valid matching url exist in network loaded fonts. + if (!allFonts[fontFamily]) { + allFonts[fontFamily] = { + type: 'hosted', + variations: [], + elementCount: { + aboveFold: aboveElements.length, + belowFold: belowElements.length, + total: elements.length + }, + urlCount: { + aboveFold: new Set(), + belowFold: new Set() + } + }; + } + + // Add variation with correct element counts + allFonts[fontFamily].variations.push({ + weight: variation.weight, + style: variation.style, + url: matchingUrl, + elementCount: { + aboveFold: aboveElements.length, + belowFold: belowElements.length, + total: elements.length + } + }); + if (aboveElements.length > 0) { allFonts[fontFamily].urlCount.aboveFold.add(matchingUrl); } @@ -559,12 +560,14 @@ class BeaconPreloadFonts { return; } - // Copy to hostedFontsResults - hostedFontsResults[fontFamily] = { - variations: allFonts[fontFamily].variations, - elementCount: { ...allFonts[fontFamily].elementCount }, - urlCount: { ...allFonts[fontFamily].urlCount }, - }; + if (allFonts[fontFamily]) { + // Copy to hostedFontsResults + hostedFontsResults[fontFamily] = { + variations: allFonts[fontFamily].variations, + elementCount: { ...allFonts[fontFamily].elementCount }, + urlCount: { ...allFonts[fontFamily].urlCount }, + }; + } } }); }