Skip to content

Spread Operators do not work in _document.js when applied to JSX #26547

@ScriptedAlchemy

Description

@ScriptedAlchemy

What version of Next.js are you using?

11.0.1

What version of Node.js are you using?

12.18.1

What browser are you using?

N/A

What operating system are you using?

macOS

How are you deploying your application?

N/A

Describe the Bug

If i use spread operators to pass props into something in _document, like a <link component, the build fails.

I was able to replicate this on stackblitz as well as in my own applications.

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

class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx);
    return { ...initialProps };
  }

  render() {
    const things = { className: 'test' };
    return (
      <Html>
        <Head />
        <link {...things} />
        <body>
        <Main />
        <NextScript />
        </body>
      </Html>
    );
  }
}

export default MyDocument;

Get the following error

Failed to compile.

./pages/_document.js
Module parse failed: Cannot read property 'forEach' of undefined
File was processed with these loaders:
 * ./node_modules/next/dist/build/babel/loader/index.js
You may need an additional loader to handle the result of these loaders.
TypeError: Cannot read property 'forEach' of undefined


> Build error occurred
Error: > Build failed because of webpack errors
    at eval (/home/projects/nextjs-hn64hn/node_modules/next/dist/build/index.js:15:924)
    at async Span.traceAsyncFn (/home/projects/nextjs-hn64hn/node_modules/next/dist/telemetry/trace/trace.js:6:584)

Expected Behavior

The build should pass

To Reproduce

Use a spread operator on a html element in _document

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions