-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds eslint rule to preconnect to Google fonts domain
- Loading branch information
1 parent
bab2288
commit 599f844
Showing
5 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Google Font Preconnect | ||
|
||
### Why This Error Occurred | ||
|
||
A preconnect resource hint was not used with a request to the Google Fonts domain. Adding `preconnect` is recommended to initiate an early connection to the origin. | ||
|
||
### Possible Ways to Fix It | ||
|
||
Add `rel="preconnect"` to the Google Font domain `<link>` tag: | ||
|
||
```jsx | ||
<link rel="preconnect" href="https://fonts.gstatic.com" /> | ||
``` | ||
|
||
### Useful Links | ||
|
||
- [Preconnect to required origins](https://web.dev/uses-rel-preconnect/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/eslint-plugin-next/lib/rules/google-font-preconnect.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const NodeAttributes = require('../utils/nodeAttributes.js') | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Ensure preconnect is used with Google Fonts', | ||
recommended: true, | ||
}, | ||
}, | ||
create: function (context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
if (node.name.name !== 'link') { | ||
return | ||
} | ||
|
||
const attributes = new NodeAttributes(node) | ||
if (!attributes.has('href') || !attributes.hasValue('href')) { | ||
return | ||
} | ||
|
||
const hrefValue = attributes.value('href') | ||
const preconnectMissing = | ||
!attributes.has('rel') || | ||
!attributes.hasValue('rel') || | ||
attributes.value('rel') !== 'preconnect' | ||
|
||
if ( | ||
hrefValue.includes('https://fonts.gstatic.com') && | ||
preconnectMissing | ||
) { | ||
context.report({ | ||
node, | ||
message: `Preconnect is missing. See https://nextjs.org/docs/messages/google-font-preconnect.`, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
60 changes: 60 additions & 0 deletions
60
test/eslint-plugin-next/google-font-preconnect.unit.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const rule = require('@next/eslint-plugin-next/lib/rules/google-font-preconnect') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
modules: true, | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('google-font-preconnect', rule, { | ||
valid: [ | ||
`export const Test = () => ( | ||
<div> | ||
<link rel="preconnect" href="https://fonts.gstatic.com"/> | ||
</div> | ||
) | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
export const Test = () => ( | ||
<div> | ||
<link href="https://fonts.gstatic.com"/> | ||
</div> | ||
) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Preconnect is missing. See https://nextjs.org/docs/messages/google-font-preconnect.', | ||
type: 'JSXOpeningElement', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
export const Test = () => ( | ||
<div> | ||
<link rel="preload" href="https://fonts.gstatic.com"/> | ||
</div> | ||
) | ||
`, | ||
errors: [ | ||
{ | ||
message: | ||
'Preconnect is missing. See https://nextjs.org/docs/messages/google-font-preconnect.', | ||
type: 'JSXOpeningElement', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |