Skip to content
Permalink
Newer
Older
100644 66 lines (60 sloc) 1.71 KB
Aug 23, 2017
1
import React from 'react';
2
import Document, { 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>
22
</html>
23
);
24
}
25
}
27
MyDocument.getInitialProps = async ctx => {
28
// Resolution order
29
//
30
// On the server:
31
// 1. app.getInitialProps
32
// 2. page.getInitialProps
33
// 3. document.getInitialProps
34
// 4. app.render
35
// 5. page.render
36
// 6. document.render
37
//
38
// On the server with error:
39
// 1. document.getInitialProps
40
// 2. app.render
41
// 3. page.render
42
// 4. document.render
43
//
44
// On the client
45
// 1. app.getInitialProps
46
// 2. page.getInitialProps
47
// 3. app.render
48
// 4. page.render
49
50
// Render app and page and get the context of the page with collected side effects.
51
const sheets = new ServerStyleSheets();
52
const originalRenderPage = ctx.renderPage;
54
ctx.renderPage = () =>
55
originalRenderPage({
56
enhanceApp: App => props => sheets.collect(<App {...props} />),
57
});
59
const initialProps = await Document.getInitialProps(ctx);
61
return {
63
// Styles fragment is rendered after the app and page rendering finish.
64
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
65
};
66
};