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