Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linter check for fast google fonts #906

Merged
merged 2 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/linter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ModuleRuntimeUsed } from "./rules/ModuleRuntimeUsed";
import { BlockingExtensionsPreloaded } from "./rules/BlockingExtensionsPreloaded";
import { FontsArePreloaded } from "./rules/FontsArePreloaded";
import { HeroImageIsDefined } from "./rules/HeroImageIsDefined";
import { FastGoogleFontsDisplay } from "./rules/FastGoogleFontsDisplay";
import { RuleConstructor } from "./rule";
import { isArray } from "util";

Expand Down Expand Up @@ -133,6 +134,7 @@ function testsForMode(type: LintMode) {
RuntimeIsPreloaded,
BlockingExtensionsPreloaded,
FontsArePreloaded,
FastGoogleFontsDisplay,
IsTransformedAmp,
ModuleRuntimeUsed,
HeroImageIsDefined,
Expand Down
40 changes: 40 additions & 0 deletions packages/linter/src/rules/FastGoogleFontsDisplay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Context } from "../index";
import { Rule } from "../rule";

const GOOGLE_FONT_URL_PATTERN = /https?:\/\/fonts.googleapis.com\/css\?(?!(?:[\s\S]+&)?display=(?:swap|fallback|optional)(?:&|$))/i;

/**
* This test will return an info when font-face definitions with a url
* but no fonts are preloaded.
* Font definitions with url but an additional font-display setting are ignored.
*/
export class FastGoogleFontsDisplay extends Rule {
run({ $ }: Context) {
const fonts = $(
"link[rel='stylesheet'][href^='https://fonts.googleapis.com/css']"
);
if (!fonts.length) {
return this.pass();
}
const results = [];
fonts.each((i, linkNode) => {
const href = $(linkNode).attr("href");
const match = GOOGLE_FONT_URL_PATTERN.exec(href);
if (match) {
results.push(
this.warn(
`Use &display=swap|fallback|optional in Google Font stylesheet URL: ${href}`
)
);
}
});
return Promise.all(results);
}
meta() {
return {
url: "https://web.dev/font-display/",
title: "Use fast font-display for Google Fonts",
info: "",
};
}
}
25 changes: 24 additions & 1 deletion packages/linter/tests/local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { IsTransformedAmp } from "../src/rules/IsTransformedAmp";
import { ModuleRuntimeUsed } from "../src/rules/ModuleRuntimeUsed";
import { BlockingExtensionsPreloaded } from "../src/rules/BlockingExtensionsPreloaded";
import { FontsArePreloaded } from "../src/rules/FontsArePreloaded";
import { HeroImageIsDefined } from '../src/rules/HeroImageIsDefined';
import { HeroImageIsDefined } from "../src/rules/HeroImageIsDefined";
import { FastGoogleFontsDisplay } from "../src/rules/FastGoogleFontsDisplay";

describe(AmpImgAmpPixelPreferred.name, () => {
it(`${AmpImgAmpPixelPreferred.name} - <amp-img height="1" width="1">`, async () => {
Expand Down Expand Up @@ -100,6 +101,28 @@ describe(BlockingExtensionsPreloaded.name, () => {
});
});

describe(FastGoogleFontsDisplay.name, () => {
it(`${FastGoogleFontsDisplay.name} - all fonts have display param`, async () => {
return assertPass(
runLocalTest(
FastGoogleFontsDisplay,
`${__dirname}/local/FastGoogleFontsDisplay-1/source.html`
)
);
});
it(`${FastGoogleFontsDisplay.name} - no or wrong display param`, async () => {
const results = await runLocalTest(
FastGoogleFontsDisplay,
`${__dirname}/local/FastGoogleFontsDisplay-2/source.html`
);
expect(results).toHaveLength(4);
await assertWarn(results[0]);
await assertWarn(results[1]);
await assertWarn(results[2]);
await assertWarn(results[3]);
});
});

describe(FontsArePreloaded.name, () => {
it(`${FontsArePreloaded.name} - preload for font exists`, async () => {
return assertPass(
Expand Down
34 changes: 34 additions & 0 deletions packages/linter/tests/local/FastGoogleFontsDisplay-1/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"headline": "Open-source framework for publishing content",
"datePublished": "2015-10-07T12:02:41Z",
"image": [
"logo.jpg"
]
}
</script>
<link rel="preconnect" href="//fonts.googleapis.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Font1&display=swap">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Font2:400,700&display=fallback">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Font3&display=optional">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?display=swap&family=Font4">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?display=fallback&family=Font5:400,700">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?display=optional&family=Font6">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
</style>
</head>
<body>
<h1>Hello, AMP!</h1>
</body>
</html>
32 changes: 32 additions & 0 deletions packages/linter/tests/local/FastGoogleFontsDisplay-2/source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"headline": "Open-source framework for publishing content",
"datePublished": "2015-10-07T12:02:41Z",
"image": [
"logo.jpg"
]
}
</script>
<link rel="preconnect" href="//fonts.googleapis.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Font2:400,700&display=block">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Font3">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?display=&family=Font4">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?display=auto&family=Font5:400,700">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<style amp-custom>
</style>
</head>
<body>
<h1>Hello, AMP!</h1>
</body>
</html>