Skip to content
Permalink
Newer
Older
100644 90 lines (82 sloc) 2.23 KB
Aug 23, 2017
1
import React from 'react';
2
import PropTypes from 'prop-types';
3
import Document, { Head, Main, NextScript } from 'next/document';
4
import flush from 'styled-jsx/server';
6
class MyDocument extends Document {
8
const { pageContext } = this.props;
9
11
<html lang="en" dir="ltr">
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 name="theme-color" content={pageContext.theme.palette.primary.main} />
21
<link
22
rel="stylesheet"
23
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"
24
/>
25
</Head>
26
<body>
27
<Main />
28
<NextScript />
29
</body>
30
</html>
31
);
32
}
33
}
34
35
MyDocument.getInitialProps = ctx => {
36
// Resolution order
37
//
38
// On the server:
39
// 1. app.getInitialProps
40
// 2. page.getInitialProps
41
// 3. document.getInitialProps
42
// 4. app.render
43
// 5. page.render
44
// 6. document.render
45
//
46
// On the server with error:
47
// 1. document.getInitialProps
48
// 2. app.render
49
// 3. page.render
50
// 4. document.render
51
//
52
// On the client
53
// 1. app.getInitialProps
54
// 2. page.getInitialProps
55
// 3. app.render
56
// 4. page.render
57
58
// Render app and page and get the context of the page with collected side effects.
59
let pageContext;
60
const page = ctx.renderPage(Component => {
61
const WrappedComponent = props => {
62
pageContext = props.pageContext;
63
return <Component {...props} />;
64
};
65
66
WrappedComponent.propTypes = {
67
pageContext: PropTypes.object.isRequired,
68
};
73
return {
74
...page,
75
pageContext,
76
// Styles fragment is rendered after the app and page rendering finish.
77
styles: (
78
<React.Fragment>
79
<style
80
id="jss-server-side"
81
// eslint-disable-next-line react/no-danger
82
dangerouslySetInnerHTML={{ __html: pageContext.sheetsRegistry.toString() }}
83
/>
84
{flush() || null}
85
</React.Fragment>
86
),
87
};
88
};
89
90
export default MyDocument;