-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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/document should not be imported outside of pages/_document.js #28518
Comments
This issue has been fixed here vercel/next.js#28596 If you don't want to update to the canary versions and still keep working normally just exclude it from your .eslintrc.json rules: |
Thanks for linking the issue @Forchapeatl Doesn't look like it is related to MUI, I am closing it. |
Still the same problem after disable "rules": { "@next/next/no-document-import-in-page": "off" } My Code: .eslintrc.json { document.js export default class MyDocument extends Document { ); } } // // You can consider sharing the same emotion cache between all the SSR requests to speed up performance. ctx.renderPage = () => const initialProps = await Document.getInitialProps(ctx); return { debug log |
For me as well there is also the error of: @ognitio-technologies From what it looks like both errors are eslint relatable as it provides us with common 'rules' of how we should code our apps. |
This is the app.js page. the code taken from material-ui sample next js project reference link : https://github.com/mui-org/material-ui/blob/master/examples/nextjs/pages/_app.js import * as React from 'react'; // Client-side cache, shared for the whole session of the user in the browser. export default function MyApp(props) { return ( MyApp.propTypes = { |
Still the same issues: |
Still same issues git hub link: https://github.com/ognitio-sample-projects/nextjs-material-ui-5 |
For the record, I got the 'component definition is missing display name...' error after following the steps to fix the 'next/document should not be imported outside of pages/document...' error. I did the following to workaround. Change the line To:
|
Project : Next.js
Query: npm run build
Error: next/document should not be imported outside of pages/_document.js
Error: Component definition is missing display name react/display-name
Packgae.json
{
"name": "armcaegis-website",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@emotion/cache": "^11.4.0",
"@emotion/react": "^11.4.1",
"@emotion/server": "^11.4.0",
"@emotion/styled": "^11.3.0",
"@mui/icons-material": "^5.0.0",
"@mui/material": "^5.0.0",
"@mui/styles": "^5.0.0",
"@reduxjs/toolkit": "^1.6.1",
"clsx": "^1.1.1",
"next": "11.1.2",
"prop-types": "^15.7.2",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-redux": "^7.2.5"
},
"devDependencies": {
"eslint": "7.32.0",
"eslint-config-next": "11.1.2"
}
}
_document.js
import * as React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';
export default class MyDocument extends Document {
render() {
return (
{/* PWA primary color */}
);
}
}
//
getInitialProps
belongs to_document
(instead of_app
),// it's compatible with static-site generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
const originalRenderPage = ctx.renderPage;
// You can consider sharing the same emotion cache between all the SSR requests to speed up performance.
// However, be aware that it can have global side effects.
const cache = createEmotionCache();
const { extractCriticalToChunks } = createEmotionServer(cache);
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => <App emotionCache={cache} {...props} />,
});
const initialProps = await Document.getInitialProps(ctx);
// This is important. It prevents emotion to render invalid HTML.
// See #26561 (comment)
const emotionStyles = extractCriticalToChunks(initialProps.html);
const emotionStyleTags = emotionStyles.styles.map((style) => (
<style
data-emotion={
${style.key} ${style.ids.join(' ')}
}key={style.key}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: style.css }}
/>
));
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [
...React.Children.toArray(initialProps.styles),
...emotionStyleTags,
],
};
};
The text was updated successfully, but these errors were encountered: