Skip to content

Font optimization can result in error: "TypeError: Cannot read property 'type' of null" #32024

@dm430

Description

@dm430

What version of Next.js are you using?

12.0.4

What version of Node.js are you using?

12.22.3

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

next start

Describe the Bug

When a child element or fragment of the Head component contains a falsely value in its children then the following build/runtime error will surface,"TypeError: Cannot read property 'type' of null". This error can surface at runtime or at build time depending on the build method being used. The SWC build system will surface the error at build time while the legacy build methodology will result in a runtime error.

Expected Behavior

Falsey child elements of elements contained within the Head component should not result in a build/runtime error.

To Reproduce

  1. Place the following snippet of code in your_document.js.
<Head>
    <>
        {/* falsy child elements are not removed. This will result in 'null' when makeStylesheetInert is called recursively. */}
        {null}
        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin />
        <link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet" />
   </>
 </Head>
  1. Run yarn build.
    *Note: If you are using SWC the error will surface on step here.
  2. Run yarn start.
  3. Visit any page.

Please see the following repo for reproduction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Font (next/font)Related to Next.js Font Optimization.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions