-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[material-ui] Add new Next.js integration package #39947
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
59c2e07
Add next.js as a dependency
siriwatknp 8a13561
add appRouter and pagesRouter
siriwatknp 261032a
switch folder to nextjs13
siriwatknp c1d34cb
update appRouter
siriwatknp 28a5345
move files to separate package
siriwatknp 968c577
Revert "Add next.js as a dependency"
siriwatknp 29ffa24
adopt in mui docs
siriwatknp ae6c266
mark emotion server and cache as optional
siriwatknp a7a43a3
add document
siriwatknp c089f60
update docs
siriwatknp 113f142
fix build
siriwatknp 672785a
run docs:i18n
siriwatknp 1f22c56
update material ui nextjs alias
siriwatknp bf26d99
add material ui nextjs to codesandbox build
siriwatknp c8f4c39
fix markdown lint
siriwatknp 30ce24e
rename folders to use nextjs version at the beginning
siriwatknp cb20154
update docs
siriwatknp 1f26cd1
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp ce51139
add DocumentHeadTags
siriwatknp 2fc39bb
update docs
siriwatknp 4dfcc6a
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp 5c39893
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp bcfb7d8
update yarn.lock
siriwatknp 13db137
fix as suggested
siriwatknp 233e4d5
redirect old page to the new one
siriwatknp 25a370b
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp e2a479f
run docs:i18n
siriwatknp e67d68a
Apply suggestions from code review
siriwatknp 374e5b4
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp 2a110c7
reexport v14 using v13
siriwatknp 2052bc2
add v14 comment
siriwatknp 0ebe960
Apply suggestions from code review
siriwatknp f943604
quick writing edits
danilo-leal f4858d5
Merge branch 'master' into feat/nextjs-integration
siriwatknp d72e882
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp 2c8e2a9
revert yarn.lock
siriwatknp 3ebef69
fix material-nextjs package.json
siriwatknp dc1c3b6
replace tslint with tsc
siriwatknp b63e72a
try with node script
siriwatknp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
227 changes: 0 additions & 227 deletions
227
docs/data/material/guides/next-js-app-router/next-js-app-router.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# Next.js Integration | ||
|
||
<p class="description">Learn how to use Material UI with Next.js</p> | ||
|
||
## App Router | ||
|
||
This section walks through the Material UI integration with the Next.js [App Router](https://nextjs.org/docs/app), an evolution of the [Pages Router](#pages-router), and, currently, the recommended way of building new Next.js applications starting from version 13. | ||
|
||
### Intalling the dependencies | ||
|
||
Start by ensuring that you already have `@mui/material` and `next` installed. | ||
Then, run one of the following commands to install the dependencies: | ||
|
||
<codeblock storageKey="package-manager"> | ||
|
||
```bash npm | ||
npm install @mui/material-nextjs @emotion/cache | ||
``` | ||
|
||
```bash yarn | ||
yarn add @mui/material-nextjs @emotion/cache | ||
``` | ||
|
||
```bash pnpm | ||
pnpm add @mui/material-nextjs @emotion/cache | ||
``` | ||
|
||
</codeblock> | ||
|
||
### Configuration | ||
|
||
Inside `app/layout.tsx`, import the `AppRouterCacheProvider` and wrap all elements under the `<body>` with it: | ||
|
||
```diff | ||
// app/layout.tsx | ||
+ import { AppRouterCacheProvider } from '@mui/material-nextjs/v13-appRouter'; // or `v14-appRouter` if you are using Next.js v14 | ||
|
||
export default function RootLayout(props) { | ||
const { children } = props; | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
+ <AppRouterCacheProvider>{children}</AppRouterCacheProvider> | ||
</body> | ||
</html> | ||
); | ||
} | ||
``` | ||
|
||
### Using other styling solutions | ||
|
||
If you are using a styling solution other than Emotion to customize Material UI components, set `enableCssLayer: true` in the `options` prop: | ||
|
||
```js | ||
<AppRouterCacheProvider options={{ enableCssLayer: true }}> | ||
``` | ||
|
||
This option ensures that the styles generated by Material UI will be wrapped in a CSS `@layer mui` rule, which is overridden by anonymous layer styles when using Material UI with CSS modules, Tailwind CSS, or even plain CSS without using `@layer`. | ||
Check warning on line 58 in docs/data/material/guides/nextjs/nextjs.md GitHub Actions / runner / vale
|
||
|
||
To learn more about it, see [the MDN CSS layer documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer). | ||
|
||
## Pages Router | ||
|
||
This section walks through the Material UI integration with the Next.js [Pages Router](https://nextjs.org/docs/pages/building-your-application), for both [Server Side Rendering](https://nextjs.org/docs/pages/building-your-application/rendering/server-side-rendering) (SSR) and [Static Site Generation](https://nextjs.org/docs/pages/building-your-application/rendering/static-site-generation) (SSG). | ||
|
||
### Intalling the dependencies | ||
|
||
Start by ensuring that you already have `@mui/material` and `next` installed. | ||
Then, run one of the following commands to install the dependencies: | ||
|
||
<codeblock storageKey="package-manager"> | ||
|
||
```bash npm | ||
npm install @mui/material-nextjs @emotion/cache @emotion/server | ||
``` | ||
|
||
```bash yarn | ||
yarn add @mui/material-nextjs @emotion/cache @emotion/server | ||
``` | ||
|
||
```bash pnpm | ||
pnpm add @mui/material-nextjs @emotion/cache @emotion/server | ||
``` | ||
|
||
</codeblock> | ||
|
||
### Configuration | ||
|
||
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 | ||
+ import { DocumentHeadTags, documentGetInitialProps } from '@mui/material-nextjs/v13-pagesRouter'; // or `v14-pagesRouter` if you are using Next.js v14 | ||
|
||
export default function MyDocument(props) { | ||
return ( | ||
<Html lang="en"> | ||
<Head> | ||
+ <DocumentHeadTags {...props} /> | ||
... | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
); | ||
} | ||
|
||
+ MyDocument.getInitialProps = documentGetInitialProps; | ||
``` | ||
|
||
Then, inside `pages/_app.tsx`, import the `AppCacheProvider` component and render it as the root element: | ||
|
||
```diff | ||
+ import { AppCacheProvider } from '@mui/material-nextjs/v13-pagesRouter'; // or `v14-pages` if you are using Next.js v14 | ||
|
||
export default function MyApp(props) { | ||
return ( | ||
+ <AppCacheProvider {...props}> | ||
<Head> | ||
... | ||
</Head> | ||
... | ||
+ </AppCacheProvider> | ||
); | ||
} | ||
``` | ||
|
||
### TypeScript | ||
|
||
If you are using TypeScript, add `DocumentHeadTagsProps` to the Document's props interface: | ||
|
||
```diff | ||
+ import type { DocumentHeadTagsProps } from '@mui/material-nextjs/v13-pagesRouter'; // or `v14-pagesRouter` if you are using Next.js v14 | ||
|
||
+ export default function MyDocument(props: DocumentProps & DocumentHeadTagsProps) { | ||
... | ||
} | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very clean guide 👌