From 957fab1ce0eaa7fb938ac709bdccc26f8dcfefca Mon Sep 17 00:00:00 2001 From: Siriwat K Date: Tue, 9 Jan 2024 10:53:35 +0700 Subject: [PATCH] [examples] Use material-nextjs integration package (#40199) --- .../package.json | 1 + .../pages/_app.tsx | 18 +-- .../pages/_document.tsx | 81 +---------- .../src/createEmotionCache.ts | 19 --- .../package.json | 1 + .../pages/_app.js | 13 +- .../pages/_document.js | 73 +--------- .../src/createEmotionCache.js | 17 --- .../package.json | 1 + .../pages/_app.tsx | 18 +-- .../pages/_document.tsx | 124 ++++++---------- .../src/Link.tsx | 13 +- .../src/createEmotionCache.ts | 7 - examples/material-ui-nextjs-ts/package.json | 1 + .../material-ui-nextjs-ts/src/app/layout.tsx | 135 +++++++++--------- .../components/ThemeRegistry/EmotionCache.tsx | 94 ------------ .../ThemeRegistry/ThemeRegistry.tsx | 18 --- .../{components/ThemeRegistry => }/theme.ts | 1 + examples/material-ui-nextjs/package.json | 1 + examples/material-ui-nextjs/src/app/layout.js | 135 +++++++++--------- .../components/ThemeRegistry/EmotionCache.js | 79 ---------- .../components/ThemeRegistry/ThemeRegistry.js | 18 --- .../{components/ThemeRegistry => }/theme.js | 1 + 23 files changed, 224 insertions(+), 645 deletions(-) delete mode 100644 examples/material-ui-nextjs-pages-router-ts/src/createEmotionCache.ts delete mode 100644 examples/material-ui-nextjs-pages-router/src/createEmotionCache.js delete mode 100644 examples/material-ui-nextjs-ts-v4-v5-migration/src/createEmotionCache.ts delete mode 100644 examples/material-ui-nextjs-ts/src/components/ThemeRegistry/EmotionCache.tsx delete mode 100644 examples/material-ui-nextjs-ts/src/components/ThemeRegistry/ThemeRegistry.tsx rename examples/material-ui-nextjs-ts/src/{components/ThemeRegistry => }/theme.ts (97%) delete mode 100644 examples/material-ui-nextjs/src/components/ThemeRegistry/EmotionCache.js delete mode 100644 examples/material-ui-nextjs/src/components/ThemeRegistry/ThemeRegistry.js rename examples/material-ui-nextjs/src/{components/ThemeRegistry => }/theme.js (97%) 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) => ( -