Skip to content

Commit

Permalink
Give page specific advice on how to fix fonts (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbenz authored Mar 19, 2021
1 parent e63ce2f commit 379d08a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 8 additions & 10 deletions packages/page-experience/lib/checks/fontDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ const checkDisplayOptional = (pageData, result) => {
if (!fontface) {
continue;
}
const isGoogleFont =
fontface.mainSrc && fontface.mainSrc.startsWith('https://fonts.gstatic.com');
if (!fontface.fontDisplay) {
items.push({
font,
fix: generateSuggestion(fontface),
fix: isGoogleFont
? 'Add `&display=optional` to your Google Font import declaration. [Read more](https://web.dev/font-display/#google-fonts).'
: 'Add `font-display: optional`',
});
} else if (fontface.fontDisplay !== 'optional') {
// TODO: only show this warning if CLS > 0
items.push({
font,
fix: generateSuggestion(fontface),
fix: isGoogleFont
? `Replace \`$display=${fontface.fontDisplay}\` with \`&display: optional\` in your Google Font import declaration. [Read more](https://web.dev/font-display/#google-fonts).',`
: `Replace \`font-display: ${fontface.fontDisplay}\` with \`font-display: optional\``,
});
}
}
Expand All @@ -58,12 +64,4 @@ const checkDisplayOptional = (pageData, result) => {
});
};

function generateSuggestion(fontface) {
const isGoogleFont = fontface.mainSrc && fontface.mainSrc.startsWith('https://fonts.gstatic.com');
if (!isGoogleFont) {
return 'Add `font-display: optional` to your font-face declaration.';
}
return 'Add the `&display=optional` parameter to the end of your Google Font import declaration ([read more](https://web.dev/font-display/#google-fonts)).';
}

module.exports = checkDisplayOptional;
2 changes: 1 addition & 1 deletion packages/page-experience/lib/checks/fontPreloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const checkPreloads = (pageData, result) => {
if (!fontface.mainSrc.startsWith('https://fonts.gstatic.com')) {
items.push({
font,
fix: `Add \`<link rel="preload" href="${fontface.mainSrc}" as="font" crossorigin>\` to your \`<head>\`.`,
fix: `Add \`<link rel="preload" href="${fontface.mainSrc}" as="font" crossorigin>\`.`,
});
}
}
Expand Down

0 comments on commit 379d08a

Please # to comment.