Skip to content

[docs] Update the snippet to use latest Next.js template #45946

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 47 additions & 42 deletions docs/data/material/integrations/nextjs/nextjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,50 +175,55 @@ Inside the `pages/_document.tsx` file:
- Import `documentGetInitialProps` and use it as the Document's `getInitialProps`.
- Import `DocumentHeadTags` and render it inside the `<Head>`.

```diff title="pages/_document.tsx"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mnajdova diff is hard for copy pasting so I changed to tsx. Do you think this is a better experience? If yes, I will update the App Router guide too.

Copy link
Member

@oliviertassinari oliviertassinari Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between https://deploy-preview-45946--material-ui.netlify.app/material-ui/integrations/nextjs/#configuration-2 and https://deploy-preview-45944--material-ui.netlify.app/material-ui/integrations/nextjs/#configuration-2 It feels like the diff UX wins with a long shot for my use case: I can't copy and paste the whole demo, I have ton of custom logic in my _document.js, but with a diff, I know what to look at.

The best solution is to have CLI that applies the changes, like Shadcn does, but that's a different story #39588 and #40062.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The best solution is to have CLI that applies the changes, like Shadcn does, but that's a different story #39588 and #40062.

As you said, it's a different story.

For this PR, the change display the smallest snippet to demonstrate the integration. I think it's better than the previous one.

+import {
+ DocumentHeadTags,
+ documentGetInitialProps,
+} from '@mui/material-nextjs/v15-pagesRouter';
// or `v1X-pagesRouter` if you are using Next.js v1X

export default function MyDocument(props) {
return (
<Html lang="en">
<Head>
+ <DocumentHeadTags {...props} />
...
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
```tsx title="pages/_document.tsx"
import {
Html,
Head,
Main,
NextScript,
DocumentContext,
DocumentProps,
} from 'next/document';
import {
DocumentHeadTags,
DocumentHeadTagsProps,
documentGetInitialProps,
} from '@mui/material-nextjs/v15-pagesRouter';

export default function Document(props: DocumentProps & DocumentHeadTagsProps) {
return (
<Html lang="en">
<Head>
<DocumentHeadTags {...props} />
</Head>
<body className="antialiased">
<Main />
<NextScript />
</body>
</Html>
);
}

+MyDocument.getInitialProps = async (ctx) => {
+ const finalProps = await documentGetInitialProps(ctx);
+ return finalProps;
+};
Document.getInitialProps = async (ctx: DocumentContext) => {
const finalProps = await documentGetInitialProps(ctx);
return finalProps;
};
```

Then, inside `pages/_app.tsx`, import the `AppCacheProvider` component and render it as the root element:

```diff title="pages/_app.tsx"
+import { AppCacheProvider } from '@mui/material-nextjs/v15-pagesRouter';
// Or `v1X-pages` if you are using Next.js v1X
```tsx title="pages/_app.tsx"
import '@/styles/globals.css';
import type { AppProps } from 'next/app';
import { AppCacheProvider } from '@mui/material-nextjs/v15-pagesRouter';

export default function MyApp(props) {
return (
+ <AppCacheProvider {...props}>
<Head>
...
</Head>
...
+ </AppCacheProvider>
);
}
export default function App({ Component, pageProps, ...props }: AppProps) {
return (
<AppCacheProvider {...props}>
<Component {...pageProps} />
</AppCacheProvider>
);
}
```

:::info
Expand All @@ -235,7 +240,7 @@ To use a custom [Emotion cache](https://emotion.sh/docs/@emotion/cache), pass it
```diff title="pages/_document.tsx"
...

MyDocument.getInitialProps = async (ctx) => {
Document.getInitialProps = async (ctx: DocumentContext) => {
const finalProps = await documentGetInitialProps(ctx, {
+ emotionCache: createCustomCache(),
});
Expand All @@ -251,7 +256,7 @@ To enable [cascade layers](https://developer.mozilla.org/en-US/docs/Learn_web_de
+import { createEmotionCache } from '@mui/material-nextjs/v15-pagesRouter';
...

MyDocument.getInitialProps = async (ctx) => {
Document.getInitialProps = async (ctx: DocumentContext) => {
const finalProps = await documentGetInitialProps(ctx, {
+ emotionCache: createEmotionCache({ enableCssLayer: true }),
});
Expand All @@ -263,9 +268,9 @@ To enable [cascade layers](https://developer.mozilla.org/en-US/docs/Learn_web_de
+import { createEmotionCache } from '@mui/material-nextjs/v15-pagesRouter';
...

const clientCache = createEmotionCache({ enableCssLayer: true });
+ const clientCache = createEmotionCache({ enableCssLayer: true });

+ export default function MyApp({ emotionCache = clientCache }) {
+ export default function App({ emotionCache = clientCache }) {
return (
+ <AppCacheProvider emotionCache={emotionCache}>
<Head>
Expand Down
Loading