Permalink
material-ui/examples/nextjs/pages/_document.js
Newer
100644
99 lines (90 sloc)
2.39 KB
2
import PropTypes from 'prop-types';
3
import Document, { Head, Main, NextScript } from 'next/document';
4
import flush from 'styled-jsx/server';
12
<Head>
13
<meta charSet="utf-8" />
14
{/* Use minimum-scale=1 to enable GPU rasterization */}
15
<meta
16
name="viewport"
17
content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
18
/>
19
{/* PWA primary color */}
20
<meta
21
name="theme-color"
22
content={pageContext ? pageContext.theme.palette.primary.main : null}
23
/>
24
<link
25
rel="stylesheet"
26
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"
27
/>
28
</Head>
29
<body>
30
<Main />
31
<NextScript />
32
</body>
33
</html>
34
);
35
}
36
}
37
38
MyDocument.getInitialProps = ctx => {
39
// Resolution order
40
//
41
// On the server:
42
// 1. app.getInitialProps
43
// 2. page.getInitialProps
44
// 3. document.getInitialProps
45
// 4. app.render
46
// 5. page.render
47
// 6. document.render
50
// 1. document.getInitialProps
51
// 2. app.render
52
// 3. page.render
53
// 4. document.render
54
//
55
// On the client
56
// 1. app.getInitialProps
57
// 2. page.getInitialProps
58
// 3. app.render
59
// 4. page.render
60
61
// Render app and page and get the context of the page with collected side effects.
62
let pageContext;
63
const page = ctx.renderPage(Component => {
64
const WrappedComponent = props => {
65
pageContext = props.pageContext;
66
return <Component {...props} />;
67
};
68
69
WrappedComponent.propTypes = {
70
pageContext: PropTypes.object.isRequired,
71
};
73
return WrappedComponent;
74
});
76
let css;
77
// It might be undefined, e.g. after an error.
78
if (pageContext) {
79
css = pageContext.sheetsRegistry.toString();
80
}
81
85
// Styles fragment is rendered after the app and page rendering finish.
87
<React.Fragment>
88
<style
89
id="jss-server-side"
90
// eslint-disable-next-line react/no-danger
91
dangerouslySetInnerHTML={{ __html: css }}
92
/>
93
{flush() || null}
94
</React.Fragment>