Skip to content

Commit ef13559

Browse files
authored
feat(material/schematics): add preconnect links for Google Fonts in index.html (#32238)
Add `<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>` and `<link rel="preconnect" href="https://fonts.googleapis.com">` hints to the generated index.html to improve font load performance
1 parent 39b5f4e commit ef13559

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/material/schematics/ng-add/fonts/material-fonts.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,20 @@ export function addFontsToIndex(options: Schema): Rule {
2626
throw new SchematicsException('No project index HTML file could be found.');
2727
}
2828

29+
const preconnectLinks = [
30+
'<link rel="preconnect" href="https://fonts.googleapis.com">',
31+
'<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>',
32+
];
33+
2934
const fonts = [
3035
'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap',
3136
'https://fonts.googleapis.com/icon?family=Material+Icons',
3237
];
3338

3439
projectIndexFiles.forEach(indexFilePath => {
40+
preconnectLinks.forEach(link => {
41+
appendHtmlElementToHead(host, indexFilePath, link);
42+
});
3543
fonts.forEach(font => {
3644
appendHtmlElementToHead(host, indexFilePath, `<link href="${font}" rel="stylesheet">`);
3745
});

src/material/schematics/ng-add/index.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ describe('ng-add schematic', () => {
144144
// Ensure that the indentation has been determined properly. We want to make sure that
145145
// the created links properly align with the existing HTML. Default CLI projects use an
146146
// indentation of two columns.
147+
expect(htmlContent).toContain(
148+
' <link rel="preconnect" href="https://fonts.googleapis.com">',
149+
);
150+
expect(htmlContent).toContain(
151+
' <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>',
152+
);
147153
expect(htmlContent).toContain(
148154
' <link href="https://fonts.googleapis.com/icon?family=Material+Icons"',
149155
);

0 commit comments

Comments
 (0)