Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next is not recognizing _document.tsx file #28786

Closed
Anam0927 opened this issue Sep 4, 2021 · 13 comments
Closed

Next is not recognizing _document.tsx file #28786

Anam0927 opened this issue Sep 4, 2021 · 13 comments
Labels
bug Issue was opened via the bug report template.

Comments

@Anam0927
Copy link

Anam0927 commented Sep 4, 2021

What version of Next.js are you using?

11.1.2

What version of Node.js are you using?

14.17.1

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying your application?

next start

Describe the Bug

Here's my _document.tsx file

import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets, StylesProvider } from '@material-ui/core/styles';
import React from 'react';

export default class MyDocument extends Document {
  render(): JSX.Element {
    return (
      <Html lang="en">
        <Head>
          <link rel="preconnect" href="https://fonts.gstatic.com" />
          <link
            href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800;900&family=Roboto:wght@500&display=swap"
            rel="stylesheet"
          />
          <script
            dangerouslySetInnerHTML={{
              __html: `(function (w, d, s, l, i) {
              w[l] = w[l] || [];
              w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
              const f = d.getElementsByTagName(s)[0],
                j = d.createElement(s),
                dl = l != 'dataLayer' ? '&l=' + l : '';
              j.async = true;
              j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
              f.parentNode.insertBefore(j, f);
            })(window, document, 'script', 'dataLayer', 'xxxxx')`,
            }}
          ></script>
        </Head>
        <body>
          <noscript
            dangerouslySetInnerHTML={{
              __html: `<iframe src="https://www.googletagmanager.com/ns.html?id=xxxxx"
height="0" width="0" style="display:none;visibility:hidden"></iframe>`,
            }}
          ></noscript>
          <StylesProvider injectFirst>
            <Main />
            <NextScript />
          </StylesProvider>
        </body>
      </Html>
    );
  }
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
  // Resolution order
  //
  // On the server:
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. document.getInitialProps
  // 4. app.render
  // 5. page.render
  // 6. document.render
  //
  // On the server with error:
  // 1. document.getInitialProps
  // 2. app.render
  // 3. page.render
  // 4. document.render
  //
  // On the client
  // 1. app.getInitialProps
  // 2. page.getInitialProps
  // 3. app.render
  // 4. page.render

  // Render app and page and get the context of the page with collected side effects.
  const sheets = new ServerStyleSheets();
  const originalRenderPage = ctx.renderPage;

  ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
    });

  const initialProps = await Document.getInitialProps(ctx);

  return {
    ...initialProps,
    // Styles fragment is rendered after the app and page rendering finish.
    styles: [sheets.getStyleElement(), ...React.Children.toArray(initialProps.styles)],
  };
};

Everything was working fine till I upgraded Next from 11.1.0 to 11.1.2
Now, build error comes stating the following:

./pages/_document.tsx
1:1  Error: next/document should not be imported outside of pages/_document.js. See https://nextjs.org/docs/messages/no-document-import-in-page.  @next/next/no-document-import-in-page
15:11  Warning: Use the `next/script` component for loading third party scripts. See: https://nextjs.org/docs/messages/next-script-for-ga.  @next/next/next-script-for-ga
78:28  Error: Component definition is missing display name  react/display-name

What's the problem here? It is in the pages folder and everything was working fine. Now, even if I go back to 11.1.0, the error still comes!!

Expected Behavior

There should be no error as the file is correctly placed with content also correctly written

To Reproduce

Create a custom _document.tsx file in a typescript next project and try to build the project.

@Anam0927 Anam0927 added the bug Issue was opened via the bug report template. label Sep 4, 2021
@rshtj
Copy link

rshtj commented Sep 4, 2021

Can confirm, even I am having the same issue

@N0tExisting
Copy link

This also happens with global styles & _app.tsx

@RaenonX
Copy link

RaenonX commented Sep 5, 2021

Confirming the same issue

@Poujhit
Copy link

Poujhit commented Sep 6, 2021

This is a issue with eslint config that nextjs 11.1.2 uses. eslint-config-next 11.1.2 causes this issue. Try downgrading that or use the canary version of the plugin.

@Anam0927
Copy link
Author

Anam0927 commented Sep 6, 2021

This is a issue with eslint config that nextjs 11.1.2 uses. eslint-config-next 11.1.2 causes this issue. Try downgrading that or use the canary version of the plugin.

The latest canary version of the plugin (11.1.3-canary.7) does not solve the issue, but downgrading to 11.1.0 does solve the issue. Thanks!

The react/display-name error still exists though. Any idea how I can remove that? This is the code where the error occurs:

ctx.renderPage = () =>
    originalRenderPage({
      enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
    });

The squiggly red line in VSCode comes for this part (props) => sheets.collect(<App {...props} />)

@stefanprobst
Copy link
Contributor

The react/display-name error still exists though

you can choose to ignore eslint errors by adding an inline comment (see here), or you can globally disable the rule in your .eslintrc.json file (see here).

@balazsorban44
Copy link
Member

Or you can convert it accordingly to a named function:

ctx.renderPage = () =>
  originalRenderPage({
    enhanceApp: (App) =>
      function EnhancedApp(props) {
        return sheets.collect(<App {...props} />)
      },
  })

This is not a Next.js-specific problem per se, I see you are using Material UI. https://github.com/mui-org/material-ui/blob/master/examples/nextjs/pages/_document.js#L58

They should be probably made aware of this. Either adding a an ESLint ignore on their example above that line or at least comment on it.

@Anam0927
Copy link
Author

Anam0927 commented Sep 7, 2021

Or you can convert it accordingly to a named function:

ctx.renderPage = () =>
  originalRenderPage({
    enhanceApp: (App) =>
      function EnhancedApp(props) {
        return sheets.collect(<App {...props} />)
      },
  })

This is not a Next.js-specific problem per se, I see you are using Material UI. https://github.com/mui-org/material-ui/blob/master/examples/nextjs/pages/_document.js#L58

They should be probably made aware of this. Either adding a an ESLint ignore on their example above that line or at least comment on it.

Thanks a lot for this!

@Anam0927 Anam0927 closed this as completed Sep 7, 2021
@omBratteng
Copy link
Contributor

This PR should fix it, but it has not been released yet
jsx-eslint/eslint-plugin-react#3065

@tr1s
Copy link

tr1s commented Sep 19, 2021

Or you can convert it accordingly to a named function:

ctx.renderPage = () =>
  originalRenderPage({
    enhanceApp: (App) =>
      function EnhancedApp(props) {
        return sheets.collect(<App {...props} />)
      },
  })

This is not a Next.js-specific problem per se, I see you are using Material UI. https://github.com/mui-org/material-ui/blob/master/examples/nextjs/pages/_document.js#L58

They should be probably made aware of this. Either adding a an ESLint ignore on their example above that line or at least comment on it.

This fix did not work for me, I just get page building errors instead:

CleanShot 2021-09-19 at 11 06 16@2x

Also @omBratteng that PR is merged and all my packages are up to date but my issue still persists.

Edit* I missed the note about downgrading eslint-config-next from 11.2.1 to 11.1.0, this fixed my build error. The react/display-name error still exists on the file though like mentioned above.

I am curious to know when I can upgrade without having problems again though.

Cheers!

@omBratteng
Copy link
Contributor

@tr1s if you install eslint-config-next to 11.2.1 and then install eslint-plugin-react separately, does that work?

@tr1s
Copy link

tr1s commented Sep 22, 2021

@tr1s if you install eslint-config-next to 11.2.1 and then install eslint-plugin-react separately, does that work?

I actually just tested out updating eslint-config-next to 11.2.1 again without installing eslint-plugin-react and it's working fine with no deployment issues or errors now.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

9 participants