Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
material-ui/examples/nextjs/pages/_document.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
78 lines (71 sloc)
1.96 KB
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
import React from 'react'; | |
import Document, { Head, Main, NextScript } from 'next/document'; | |
import { ServerStyleSheets } from '@material-ui/styles'; | |
import theme from '../src/theme'; | |
class MyDocument extends Document { | |
render() { | |
return ( | |
<html lang="en"> | |
<Head> | |
<meta charSet="utf-8" /> | |
<meta | |
name="viewport" | |
content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no" | |
/> | |
{/* PWA primary color */} | |
<meta name="theme-color" content={theme.palette.primary.main} /> | |
<link | |
rel="stylesheet" | |
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" | |
/> | |
</Head> | |
<body> | |
<Main /> | |
<NextScript /> | |
</body> | |
</html> | |
); | |
} | |
} | |
MyDocument.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 sheets = new ServerStyleSheets(); | |
const originalRenderPage = ctx.renderPage; | |
ctx.renderPage = () => | |
originalRenderPage({ | |
enhanceApp: App => props => sheets.collect(<App {...props} />), | |
}); | |
const initialProps = await Document.getInitialProps(ctx); | |
return { | |
...initialProps, | |
// Styles fragment is rendered after the app and page rendering finish. | |
styles: [ | |
<React.Fragment key="styles"> | |
{initialProps.styles} | |
{sheets.getStyleElement()} | |
</React.Fragment>, | |
], | |
}; | |
}; | |
export default MyDocument; |