Skip to content
Permalink
Newer
Older
100644 138 lines (130 sloc) 4.72 KB
1
import React from 'react';
2
import Document, { Head, Main, NextScript } from 'next/document';
3
import postcss from 'postcss';
4
import autoprefixer from 'autoprefixer';
5
import cssnano from 'cssnano';
6
import getPageContext from 'docs/src/modules/styles/getPageContext';
7
import config from 'docs/src/config';
9
// You can find a benchmark of the available CSS minifiers under
10
// https://github.com/GoalSmashers/css-minification-benchmark
11
// We have found that clean-css is faster than cssnano but the output is larger.
12
// Waiting for https://github.com/cssinjs/jss/issues/279
13
// 4% slower but 12% smaller output than doing it in a single step.
14
//
15
// It's using .browserslistrc
16
const prefixer = postcss([autoprefixer]);
17
const minifier = postcss([cssnano]);
18
19
class MyDocument extends Document {
20
render() {
21
const { canonical, pageContext } = this.props;
23
return (
24
<html lang="en" dir="ltr">
25
<Head>
26
<meta
27
name="description"
28
content="React Components that Implement Google's Material Design."
29
/>
30
{/* Use minimum-scale=1 to enable GPU rasterization */}
31
<meta
32
name="viewport"
33
content={
34
'user-scalable=0, initial-scale=1, ' +
35
'minimum-scale=1, width=device-width, height=device-height'
36
}
37
/>
38
{/*
39
manifest.json provides metadata used when your web app is added to the
40
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
41
*/}
42
<link rel="manifest" href="/static/manifest.json" />
43
{/* PWA primary color */}
44
<meta name="theme-color" content={pageContext.theme.palette.primary.main} />
45
<link
46
rel="stylesheet"
47
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"
48
/>
49
<style id="insertion-point-jss" />
50
{/* Twitter */}
51
<meta name="twitter:card" content="summary" />
52
<meta name="twitter:site" content="@MaterialUI" />
53
<meta name="twitter:title" content="Material-UI" />
54
<meta
55
name="twitter:description"
56
content="React Components that Implement Google's Material Design."
57
/>
58
<meta name="twitter:image" content="https://material-ui-next.com/static/brand.png" />
59
{/* Facebook */}
60
<meta property="og:type" content="website" />
61
<meta property="og:title" content="Material-UI" />
62
<meta
63
property="og:description"
64
content="React Components that Implement Google's Material Design."
65
/>
66
<meta property="og:image" content="https://material-ui-next.com/static/brand.png" />
67
<meta property="og:locale" content="en_US" />
68
<link rel="shortcut icon" href="/static/favicon.ico" />
69
<link rel="canonical" href={canonical} />
70
</Head>
71
<body>
72
<Main />
73
{/* Global Site Tag (gtag.js) - Google Analytics */}
74
<script async src={`https://www.googletagmanager.com/gtag/js?id=${config.google.id}`} />
75
<script
76
// eslint-disable-next-line react/no-danger
77
dangerouslySetInnerHTML={{
78
__html: `
79
window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments)};
80
gtag('js', new Date());
81
`,
82
}}
83
/>
84
<NextScript />
85
<script async src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js" />
86
</body>
87
</html>
88
);
89
}
90
}
91
92
MyDocument.getInitialProps = async ctx => {
93
// Resolution order
94
//
95
// On the server:
96
// 1. page.getInitialProps
97
// 2. document.getInitialProps
98
// 3. page.render
99
// 4. document.render
100
//
101
// On the server with error:
102
// 2. document.getInitialProps
103
// 3. page.render
104
// 4. document.render
105
//
106
// On the client
107
// 1. page.getInitialProps
108
// 3. page.render
109
110
// Get the context of the page to collected side effects.
111
const pageContext = getPageContext();
112
const page = ctx.renderPage(Component => props => (
113
<Component pageContext={pageContext} {...props} />
116
let css = pageContext.sheetsRegistry.toString();
117
if (process.env.NODE_ENV === 'production') {
118
const result1 = await prefixer.process(css, { from: undefined });
119
css = result1.css;
120
const result2 = await minifier.process(css, { from: undefined });
121
css = result2.css;
124
return {
125
...page,
126
pageContext,
127
canonical: `https://material-ui-next.com${ctx.req.url.replace(/\/$/, '')}/`,
128
styles: (
129
<style
130
id="jss-server-side"
131
// eslint-disable-next-line react/no-danger
132
dangerouslySetInnerHTML={{ __html: css }}
133
/>
134
),
135
};
136
};
137
138
export default MyDocument;