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

Do not preload unused variants in a self-hosting scenario #102

Merged
merged 1 commit into from
Jul 5, 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
5 changes: 4 additions & 1 deletion lib/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,10 @@ These glyphs are used on your site, but they don't exist in the font you applied
for (const fontRelation of cssAsset.outgoingRelations) {
const fontAsset = fontRelation.to;

if (fontAsset.contentType === 'font/woff2') {
if (
fontAsset.contentType === 'font/woff2' &&
fontRelation.href.startsWith('/subfont/')
) {
// Only <link rel="preload"> for woff2 files
// Preload support is a subset of woff2 support:
// - https://caniuse.com/#search=woff2
Expand Down
42 changes: 38 additions & 4 deletions test/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3653,17 +3653,51 @@ describe('subsetFonts', function () {
text: { $regex: /new FontFace/ },
});
expect(preloadPolyfill.text, 'to contain', ".woff2'")
.and('to contain', ".woff'")
.and('not to contain', ".ttf'")
.and('not to contain', 'fonts.gstatic.com');
.and('to contain', 'Input_Mono-400')
.and('not to contain', 'Input_Mono-700');
const preloadLinks = assetGraph.findRelations({
from: htmlAsset,
type: 'HtmlPreloadLink',
});
expect(preloadLinks, 'to satisfy', [
{ href: /^\/subfont\/Noto_Serif-400-[a-f0-9]{10}\.woff2$/ },
{ href: /^\/subfont\/Input_Mono-400-[a-f0-9]{10}\.woff2$/ },
]);
});

describe('with Google Web Fonts', function () {
it('should not preload the unused variants', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'../testdata/subsetFonts/unused-variant-preload-google/'
),
});
const [htmlAsset] = await assetGraph.loadAssets('index.html');
await assetGraph.populate({
followRelations: {
crossorigin: false,
},
});
await subsetFonts(assetGraph, {
inlineFonts: false,
});
const [preloadPolyfill] = assetGraph.findAssets({
type: 'JavaScript',
text: { $regex: /new FontFace/ },
});
expect(preloadPolyfill.text, 'to contain', ".woff2'")
.and('to contain', ".woff'")
.and('not to contain', ".ttf'")
.and('not to contain', 'fonts.gstatic.com');
const preloadLinks = assetGraph.findRelations({
from: htmlAsset,
type: 'HtmlPreloadLink',
});
expect(preloadLinks, 'to satisfy', [
{ href: /^\/subfont\/Noto_Serif-400-[a-f0-9]{10}\.woff2$/ },
]);
});
});
});

it('should return a fontInfo object with defaulted/normalized props', async function () {
Expand Down
9 changes: 9 additions & 0 deletions testdata/subsetFonts/unused-variant-preload-google/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Noto+Serif:400,700,400i|Open+Sans:700,400">
</head>
<body>
<div style="font-family: Noto Serif">Hello</div>
</body>
</html>
Binary file not shown.
Binary file not shown.
23 changes: 19 additions & 4 deletions testdata/subsetFonts/unused-variant-preload/index.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
<!DOCTYPE html>
<html>
<!doctype html>
<html class="fonts-loaded">
<head>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Noto+Serif:400,700,400i|Open+Sans:700,400">
<style>
@font-face {
font-family: Input Mono;
src: url(InputMono-Regular.woff2) format("woff2");
}

@font-face {
font-family: Input Mono;
src: url(InputMono-Medium.woff2) format("woff2");
font-weight: 700;
}

code {
font-family: Input Mono, monospace;
}
</style>
</head>
<body>
<div style="font-family: Noto Serif">Hello</div>
<code>foo</code>
</body>
</html>