diff --git a/examples/material-ui-nextjs-pages-router-ts/package.json b/examples/material-ui-nextjs-pages-router-ts/package.json index 00a91c1360664b..1b522774bc1e53 100644 --- a/examples/material-ui-nextjs-pages-router-ts/package.json +++ b/examples/material-ui-nextjs-pages-router-ts/package.json @@ -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" diff --git a/examples/material-ui-nextjs-pages-router-ts/pages/_app.tsx b/examples/material-ui-nextjs-pages-router-ts/pages/_app.tsx index f41dbacff76eaf..9758b017cebfad 100644 --- a/examples/material-ui-nextjs-pages-router-ts/pages/_app.tsx +++ b/examples/material-ui-nextjs-pages-router-ts/pages/_app.tsx @@ -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 ( - + @@ -26,6 +18,6 @@ export default function MyApp(props: MyAppProps) { - + ); } diff --git a/examples/material-ui-nextjs-pages-router-ts/pages/_document.tsx b/examples/material-ui-nextjs-pages-router-ts/pages/_document.tsx index e61175113c0151..8e42b2b7ecabd0 100644 --- a/examples/material-ui-nextjs-pages-router-ts/pages/_document.tsx +++ b/examples/material-ui-nextjs-pages-router-ts/pages/_document.tsx @@ -1,23 +1,9 @@ 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 ( @@ -25,7 +11,7 @@ export default function MyDocument({ emotionStyleTags }: MyDocumentProps) { - {emotionStyleTags} +
@@ -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 & MyAppProps>) => - function EnhanceApp(props) { - return ; - }, - }); - - 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) => ( -