Permalink
material-ui/examples/nextjs/pages/_document.js
Newer
100644
68 lines (62 sloc)
1.84 KB
2
import Document, { Html, Head, Main, NextScript } from 'next/document';
3
import { ServerStyleSheets } from '@material-ui/core/styles';
4
import theme from '../src/theme';
6
export default class MyDocument extends Document {
10
<Head>
11
{/* PWA primary color */}
12
<meta name="theme-color" content={theme.palette.primary.main} />
15
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
16
/>
17
</Head>
18
<body>
19
<Main />
20
<NextScript />
21
</body>
27
// `getInitialProps` belongs to `_document` (instead of `_app`),
28
// it's compatible with server-side generation (SSG).
33
// 1. app.getInitialProps
34
// 2. page.getInitialProps
35
// 3. document.getInitialProps
36
// 4. app.render
37
// 5. page.render
38
// 6. document.render
41
// 1. document.getInitialProps
42
// 2. app.render
43
// 3. page.render
44
// 4. document.render
45
//
46
// On the client
47
// 1. app.getInitialProps
48
// 2. page.getInitialProps
49
// 3. app.render
50
// 4. page.render
51
52
// Render app and page and get the context of the page with collected side effects.
53
const sheets = new ServerStyleSheets();
54
const originalRenderPage = ctx.renderPage;
56
ctx.renderPage = () =>
57
originalRenderPage({
58
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
61
const initialProps = await Document.getInitialProps(ctx);
65
// Styles fragment is rendered after the app and page rendering finish.
66
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],