Skip to content
Permalink
Newer
Older
100644 85 lines (79 sloc) 2.17 KB
Aug 23, 2017
1
import React from 'react';
2
import Document, { Head, Main, NextScript } from 'next/document';
Sep 12, 2017
3
import JssProvider from 'react-jss/lib/JssProvider';
4
import flush from 'styled-jsx/server';
5
import getPageContext from '../src/getPageContext';
7
class MyDocument extends Document {
9
const { pageContext } = this.props;
10
12
<html lang="en" dir="ltr">
13
<Head>
14
<title>My page</title>
15
<meta charSet="utf-8" />
16
{/* Use minimum-scale=1 to enable GPU rasterization */}
17
<meta
18
name="viewport"
19
content={
20
'user-scalable=0, initial-scale=1, ' +
21
'minimum-scale=1, width=device-width, height=device-height'
22
}
23
/>
24
{/* PWA primary color */}
25
<meta name="theme-color" content={pageContext.theme.palette.primary.main} />
26
<link
27
rel="stylesheet"
28
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"
29
/>
30
</Head>
31
<body>
32
<Main />
33
<NextScript />
34
</body>
35
</html>
36
);
37
}
38
}
39
40
MyDocument.getInitialProps = ctx => {
41
// Resolution order
42
//
43
// On the server:
44
// 1. page.getInitialProps
45
// 2. document.getInitialProps
46
// 3. page.render
47
// 4. document.render
48
//
49
// On the server with error:
50
// 2. document.getInitialProps
51
// 3. page.render
52
// 4. document.render
53
//
54
// On the client
55
// 1. page.getInitialProps
56
// 3. page.render
57
58
// Get the context of the page to collected side effects.
59
const pageContext = getPageContext();
60
const page = ctx.renderPage(Component => props => (
62
registry={pageContext.sheetsRegistry}
63
generateClassName={pageContext.generateClassName}
65
<Component pageContext={pageContext} {...props} />
66
</JssProvider>
67
));
68
69
return {
70
...page,
71
pageContext,
72
styles: (
73
<React.Fragment>
74
<style
75
id="jss-server-side"
76
// eslint-disable-next-line react/no-danger
77
dangerouslySetInnerHTML={{ __html: pageContext.sheetsRegistry.toString() }}
78
/>
79
{flush() || null}
80
</React.Fragment>
81
),
82
};
83
};
84
85
export default MyDocument;