Skip to content

Commit

Permalink
[examples] Use material-nextjs integration package (#40199)
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp authored Jan 9, 2024
1 parent ea16771 commit 957fab1
Show file tree
Hide file tree
Showing 23 changed files with 224 additions and 645 deletions.
1 change: 1 addition & 0 deletions examples/material-ui-nextjs-pages-router-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@emotion/styled": "latest",
"@mui/icons-material": "latest",
"@mui/material": "latest",
"@mui/material-nextjs": "latest",
"next": "latest",
"react": "latest",
"react-dom": "latest"
Expand Down
18 changes: 5 additions & 13 deletions examples/material-ui-nextjs-pages-router-ts/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import * as React from 'react';
import Head from 'next/head';
import { AppProps } from 'next/app';
import { AppCacheProvider } from '@mui/material-nextjs/v14-pagesRouter';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { CacheProvider, EmotionCache } from '@emotion/react';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();

export interface MyAppProps extends AppProps {
emotionCache?: EmotionCache;
}

export default function MyApp(props: MyAppProps) {
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
export default function MyApp(props: AppProps) {
const { Component, pageProps } = props;
return (
<CacheProvider value={emotionCache}>
<AppCacheProvider {...props}>
<Head>
<meta name="viewport" content="initial-scale=1, width=device-width" />
</Head>
Expand All @@ -26,6 +18,6 @@ export default function MyApp(props: MyAppProps) {
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</CacheProvider>
</AppCacheProvider>
);
}
81 changes: 5 additions & 76 deletions examples/material-ui-nextjs-pages-router-ts/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import * as React from 'react';
import Document, {
Html,
Head,
Main,
NextScript,
DocumentProps,
DocumentContext,
} from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import { AppType } from 'next/app';
import { Html, Head, Main, NextScript, DocumentProps } from 'next/document';
import { DocumentHeadTags, documentGetInitialProps } from '@mui/material-nextjs/v14-pagesRouter';
import theme, { roboto } from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';
import { MyAppProps } from './_app';

interface MyDocumentProps extends DocumentProps {
emotionStyleTags: JSX.Element[];
}

export default function MyDocument({ emotionStyleTags }: MyDocumentProps) {
export default function MyDocument(props: DocumentProps) {
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}
<DocumentHeadTags {...props} />
</Head>
<body>
<Main />
Expand All @@ -35,61 +21,4 @@ export default function MyDocument({ emotionStyleTags }: MyDocumentProps) {
);
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with static-site generation (SSG).
MyDocument.getInitialProps = async (ctx: DocumentContext) => {
// 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

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: React.ComponentType<React.ComponentProps<AppType> & MyAppProps>) =>
function EnhanceApp(props) {
return <App emotionCache={cache} {...props} />;
},
});

const initialProps = await Document.getInitialProps(ctx);
// This is important. It prevents Emotion to render invalid HTML.
// See https://github.com/mui/material-ui/issues/26561#issuecomment-855286153
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,
emotionStyleTags,
};
};
MyDocument.getInitialProps = documentGetInitialProps;

This file was deleted.

1 change: 1 addition & 0 deletions examples/material-ui-nextjs-pages-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@emotion/styled": "latest",
"@mui/icons-material": "latest",
"@mui/material": "latest",
"@mui/material-nextjs": "latest",
"next": "latest",
"prop-types": "latest",
"react": "latest",
Expand Down
13 changes: 4 additions & 9 deletions examples/material-ui-nextjs-pages-router/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import Head from 'next/head';
import { AppCacheProvider } from '@mui/material-nextjs/v14-pagesRouter';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { CacheProvider } from '@emotion/react';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();

export default function MyApp(props) {
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
const { Component, pageProps } = props;

return (
<CacheProvider value={emotionCache}>
<AppCacheProvider {...props}>
<Head>
<meta name="viewport" content="initial-scale=1, width=device-width" />
</Head>
Expand All @@ -23,12 +19,11 @@ export default function MyApp(props) {
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</CacheProvider>
</AppCacheProvider>
);
}

MyApp.propTypes = {
Component: PropTypes.elementType.isRequired,
emotionCache: PropTypes.object,
pageProps: PropTypes.object.isRequired,
};
73 changes: 4 additions & 69 deletions examples/material-ui-nextjs-pages-router/pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
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 { Html, Head, Main, NextScript } from 'next/document';
import { DocumentHeadTags, documentGetInitialProps } from '@mui/material-nextjs/v14-pagesRouter';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

export default function MyDocument(props) {
const { emotionStyleTags } = props;

return (
<Html lang="en">
<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}
<DocumentHeadTags {...props} />
</Head>
<body>
<Main />
Expand All @@ -25,65 +21,4 @@ export default function MyDocument(props) {
);
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with static-site 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

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) =>
function EnhanceApp(props) {
return <App emotionCache={cache} {...props} />;
},
});

const initialProps = await Document.getInitialProps(ctx);
// This is important. It prevents Emotion to render invalid HTML.
// See https://github.com/mui/material-ui/issues/26561#issuecomment-855286153
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,
emotionStyleTags,
};
};

MyDocument.propTypes = {
emotionStyleTags: PropTypes.array.isRequired,
};
MyDocument.getInitialProps = documentGetInitialProps;
17 changes: 0 additions & 17 deletions examples/material-ui-nextjs-pages-router/src/createEmotionCache.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@emotion/styled": "latest",
"@mui/icons-material": "latest",
"@mui/material": "latest",
"@mui/material-nextjs": "latest",
"@mui/styles": "latest",
"autoprefixer": "latest",
"clean-css": "latest",
Expand Down
18 changes: 5 additions & 13 deletions examples/material-ui-nextjs-ts-v4-v5-migration/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import * as React from 'react';
import Head from 'next/head';
import { AppProps } from 'next/app';
import { AppCacheProvider } from '@mui/material-nextjs/v14-pagesRouter';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { CacheProvider, EmotionCache } from '@emotion/react';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();

export interface MyAppProps extends AppProps {
emotionCache?: EmotionCache;
}

export default function MyApp(props: MyAppProps) {
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
export default function MyApp(props: AppProps) {
const { Component, pageProps } = props;

React.useEffect(() => {
// Remove the server-side injected CSS.
Expand All @@ -26,7 +18,7 @@ export default function MyApp(props: MyAppProps) {
}, []);

return (
<CacheProvider value={emotionCache}>
<AppCacheProvider {...props}>
<Head>
<meta name="viewport" content="initial-scale=1, width=device-width" />
</Head>
Expand All @@ -35,6 +27,6 @@ export default function MyApp(props: MyAppProps) {
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</CacheProvider>
</AppCacheProvider>
);
}
Loading

0 comments on commit 957fab1

Please sign in to comment.