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