-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe28eb6
commit cfe5679
Showing
8 changed files
with
2,295 additions
and
289 deletions.
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import createCache from '@emotion/cache' | ||
|
||
export default function createEmotionCache() { | ||
return createCache({ key: 'css', prepend: true }) | ||
return createCache({ key: 'css' }) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,35 @@ | ||
/* eslint-disable @next/next/no-document-import-in-page */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import React from 'react' | ||
import Document, { Html, Head, Main, NextScript } from 'next/document' | ||
import { Html, Head, Main, NextScript, DocumentProps } from 'next/document' | ||
|
||
import theme from 'common/theme' | ||
import FaviconMetadata from 'components/common/brand/FaviconMetadata' | ||
import createEmotionServer from '@emotion/server/create-instance' | ||
import createEmotionCache from 'common/createEmotionCache' | ||
|
||
export default class CustomDocument extends Document { | ||
render() { | ||
return ( | ||
<Html> | ||
<Head> | ||
{/* PWA primary color */} | ||
<meta name="theme-color" content={theme.palette.primary.main} /> | ||
import { DocumentHeadTags, documentGetInitialProps } from '@mui/material-nextjs/v14-pagesRouter' | ||
|
||
<FaviconMetadata /> | ||
{/* Inject MUI styles first to match with the prepend: true configuration. */} | ||
{(this.props as any).emotionStyleTags} | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
interface MyDocumentProps extends DocumentProps { | ||
emotionStyleTags: JSX.Element[] | ||
} | ||
|
||
// `getInitialProps` belongs to `_document` (instead of `_app`), | ||
// it's compatible with server-side generation (SSG). | ||
CustomDocument.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 | ||
|
||
// Render app and page and get the context of the page with collected side effects. | ||
const originalRenderPage = ctx.renderPage | ||
const cache = createEmotionCache() | ||
const { extractCriticalToChunks } = createEmotionServer(cache) | ||
|
||
ctx.renderPage = () => | ||
originalRenderPage({ | ||
enhanceApp: (App: any) => | ||
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, | ||
} | ||
export default function MyDocument(props: MyDocumentProps) { | ||
return ( | ||
<Html> | ||
<Head> | ||
{/* PWA primary color */} | ||
<meta name="theme-color" content={theme.palette.primary.main} /> | ||
|
||
<FaviconMetadata /> | ||
{/* Inject MUI styles first to match with the prepend: true configuration. */} | ||
<meta name="emotion-insertion-point" content="" /> | ||
<DocumentHeadTags {...props} /> | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
|
||
MyDocument.getInitialProps = documentGetInitialProps |
Oops, something went wrong.