2021-05-10 21:08:14 +02:00
const NodeAttributes = require ( '../utils/nodeAttributes.js' )
module . exports = {
meta : {
docs : {
description :
'Recommend adding custom font in a custom document and not in a specific page' ,
recommended : true ,
} ,
} ,
create : function ( context ) {
2021-08-05 02:58:06 +02:00
let documentImportName
2021-05-10 21:08:14 +02:00
return {
ImportDeclaration ( node ) {
if ( node . source . value === 'next/document' ) {
2021-08-05 02:58:06 +02:00
const documentImport = node . specifiers . find (
( { type } ) => type === 'ImportDefaultSpecifier'
)
if ( documentImport && documentImport . local ) {
documentImportName = documentImport . local . name
2021-05-10 21:08:14 +02:00
}
}
} ,
JSXOpeningElement ( node ) {
const documentClass = context
. getAncestors ( )
. find (
( ancestorNode ) =>
ancestorNode . type === 'ClassDeclaration' &&
ancestorNode . superClass &&
2021-08-05 02:58:06 +02:00
ancestorNode . superClass . name === documentImportName
2021-05-10 21:08:14 +02:00
)
2021-08-05 02:58:06 +02:00
if ( documentClass || node . name . name !== 'link' ) {
2021-05-10 21:08:14 +02:00
return
}
const attributes = new NodeAttributes ( node )
if ( ! attributes . has ( 'href' ) || ! attributes . hasValue ( 'href' ) ) {
return
}
const hrefValue = attributes . value ( 'href' )
2021-05-12 14:24:42 +02:00
const isGoogleFont =
typeof hrefValue === 'string' &&
hrefValue . startsWith ( 'https://fonts.googleapis.com/css' )
2021-05-10 21:08:14 +02:00
if ( isGoogleFont ) {
context . report ( {
node ,
message :
2021-05-20 00:47:38 +02:00
'Custom fonts not added at the document level will only load for a single page. This is discouraged. See https://nextjs.org/docs/messages/no-page-custom-font.' ,
2021-05-10 21:08:14 +02:00
} )
}
} ,
}
} ,
}