Skip to content

Commit

Permalink
[examples] Convert Next.js _document class components to function com…
Browse files Browse the repository at this point in the history
…ponents (#36109)
  • Loading branch information
ossan-engineer authored Feb 24, 2023
1 parent b253d5b commit a425abf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 62 deletions.
58 changes: 34 additions & 24 deletions examples/material-next-ts-v4-v5-migration/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import * as React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import Document, {
Html,
Head,
Main,
NextScript,
DocumentProps,
DocumentContext,
} from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import { EmotionJSX } from '@emotion/react/types/jsx-namespace';
import { ServerStyleSheets as JSSServerStyleSheets } from '@mui/styles';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

export default class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/favicon.ico" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
{/* Inject MUI styles first to match with the prepend: true configuration. */}
{(this.props as any).emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
interface MyDocumentProps extends DocumentProps {
emotionStyleTags: EmotionJSX.Element[];
}

export default function MyDocument({ emotionStyleTags }: MyDocumentProps) {
return (
<Html lang="en">
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/favicon.ico" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
{/* Inject MUI styles first to match with the prepend: true configuration. */}
{emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}

// You can find a benchmark of the available CSS minifiers under
Expand All @@ -51,7 +61,7 @@ if (process.env.NODE_ENV === 'production') {

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with static-site generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
MyDocument.getInitialProps = async (ctx: DocumentContext) => {
// Resolution order
//
// On the server:
Expand Down
50 changes: 30 additions & 20 deletions examples/material-next-ts/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import * as React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import Document, {
Html,
Head,
Main,
NextScript,
DocumentProps,
DocumentContext,
} from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import { EmotionJSX } from '@emotion/react/types/jsx-namespace';
import theme, { roboto } from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

export default class MyDocument extends Document {
render() {
return (
<Html lang="en" className={roboto.className}>
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="emotion-insertion-point" content="" />
{(this.props as any).emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
interface MyDocumentProps extends DocumentProps {
emotionStyleTags: EmotionJSX.Element[];
}

export default function MyDocument({ emotionStyleTags }: MyDocumentProps) {
return (
<Html lang="en" className={roboto.className}>
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="emotion-insertion-point" content="" />
{emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with static-site generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
MyDocument.getInitialProps = async (ctx: DocumentContext) => {
// Resolution order
//
// On the server:
Expand Down
41 changes: 23 additions & 18 deletions examples/material-next/pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import theme, { roboto } from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

export default class MyDocument extends Document {
render() {
return (
<Html lang="en" className={roboto.className}>
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="emotion-insertion-point" content="" />
{this.props.emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
export default function MyDocument(props) {
const { emotionStyleTags } = props;

return (
<Html lang="en" className={roboto.className}>
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="emotion-insertion-point" content="" />
{emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
Expand Down Expand Up @@ -82,3 +83,7 @@ MyDocument.getInitialProps = async (ctx) => {
emotionStyleTags,
};
};

MyDocument.propTypes = {
emotionStyleTags: PropTypes.array.isRequired,
};

0 comments on commit a425abf

Please sign in to comment.