Skip to content
Permalink
Newer
Older
100644 68 lines (62 sloc) 1.84 KB
Aug 23, 2017
1
import React from 'react';
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 {
9
<Html lang="en">
10
<Head>
11
{/* PWA primary color */}
12
<meta name="theme-color" content={theme.palette.primary.main} />
13
<link
14
rel="stylesheet"
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).
29
MyDocument.getInitialProps = async (ctx) => {
30
// Resolution order
31
//
32
// On the server:
33
// 1. app.getInitialProps
34
// 2. page.getInitialProps
35
// 3. document.getInitialProps
36
// 4. app.render
37
// 5. page.render
38
// 6. document.render
39
//
40
// On the server with error:
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);
63
return {
65
// Styles fragment is rendered after the app and page rendering finish.
66
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
67
};
68
};