Skip to content

Current eslint rule no-page-custom-font can't handle functional _document #29970

@frencojobs

Description

@frencojobs

What version of Next.js are you using?

11.1.1

What version of Node.js are you using?

14.18.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

Assuming #28515 will warrant us the ability to write custom _document in functional way, the current implementation of eslint rule no-page-custom-font needs to be refactored. It seems like it is currently done via checking whether the stylesheet <link> is in a Document class or not.

ancestorNode.type === 'ClassDeclaration' &&
ancestorNode.superClass &&
ancestorNode.superClass.name === documentImportName

So, for a document like this

import {Head, Html, Main, NextScript} from 'next/document'

const Document = () => (
  <Html>
    <Head>
      <link
        href='https://fonts.googleapis.com/css2?family=Inter&display=optional'
        rel='stylesheet'
      />
    </Head>
    <body>
      <Main />
      <NextScript />
    </body>
  </Html>
)

export default Document

next lint will wrongly yield a warning like this

./pages/_document.tsx
6:7  Warning: 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.  @next/next/no-page-custom-font

Expected Behavior

next lint to not wrongly yield the warning for the functional _document.

To Reproduce

  1. Use a typical class Document with a google font <link> embedded.
  2. Run next lint and witness there's no warning.
  3. Switch the document to a functional one.
  4. Run next lint again and there should be a warning as aforementioned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LintingRelated to `next lint` or ESLint with Next.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions