Skip to content
Permalink
Newer
Older
100644 144 lines (130 sloc) 4.49 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, url } = this.props;
28
29
let font = 'https://fonts.googleapis.com/css?family=Roboto:300,400,500';
30
31
if (url.match(/onepirate/)) {
32
font = 'https://fonts.googleapis.com/css?family=Roboto+Condensed:700|Work+Sans:300,400';
33
}
35
return (
36
<html lang="en" dir="ltr">
37
<Head>
38
{/* Use minimum-scale=1 to enable GPU rasterization */}
39
<meta
40
name="viewport"
41
content="minimum-scale=1, initial-scale=1, width=device-width, shrink-to-fit=no"
42
/>
43
{/*
44
manifest.json provides metadata used when your web app is added to the
45
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
46
*/}
47
<link rel="manifest" href="/static/manifest.json" />
48
{/* PWA primary color */}
49
<meta
50
name="theme-color"
51
content={pageContext ? pageContext.theme.palette.primary.main : null}
52
/>
53
<link rel="shortcut icon" href="/static/favicon.ico" />
54
<link rel="canonical" href={canonical} />
55
<link rel="stylesheet" href={font} />
56
{/*
57
Preconnect allows the browser to setup early connections before an HTTP request
58
is actually sent to the server.
59
This includes DNS lookups, TLS negotiations, TCP handshakes.
60
*/}
61
<link href="https://fonts.gstatic.com" rel="preconnect" crossOrigin="anonymous" />
62
<style id="insertion-point-jss" />
63
</Head>
64
<body>
65
<Main />
66
<NextScript />
67
{/* Global Site Tag (gtag.js) - Google Analytics */}
68
<script defer src="https://www.google-analytics.com/analytics.js" />
69
<script
70
// eslint-disable-next-line react/no-danger
71
dangerouslySetInnerHTML={{
72
__html: `
73
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
74
window.ga('create','${GOOGLE_ID}','auto');
75
`,
76
}}
77
/>
78
<script defer src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js" />
79
</body>
80
</html>
81
);
82
}
83
}
84
85
MyDocument.getInitialProps = async ctx => {
86
// Resolution order
87
//
88
// On the server:
89
// 1. page.getInitialProps
90
// 2. document.getInitialProps
91
// 3. page.render
92
// 4. document.render
93
//
94
// On the server with error:
95
// 2. document.getInitialProps
96
// 3. page.render
97
// 4. document.render
98
//
99
// On the client
100
// 1. page.getInitialProps
101
// 3. page.render
102
103
// Render app and page and get the context of the page with collected side effects.
104
let pageContext;
105
const page = ctx.renderPage(Component => {
106
const WrappedComponent = props => {
107
pageContext = props.pageContext;
108
return <Component {...props} />;
109
};
110
111
WrappedComponent.propTypes = {
112
pageContext: PropTypes.object.isRequired,
113
};
114
115
return WrappedComponent;
116
});
119
// It might be undefined, e.g. after an error.
120
if (pageContext) {
121
css = pageContext.sheetsRegistry.toString();
122
if (process.env.NODE_ENV === 'production') {
123
const result1 = await prefixer.process(css, { from: undefined });
124
css = result1.css;
125
css = cleanCSS.minify(css).styles;
126
}
129
return {
130
...page,
131
pageContext,
132
url: ctx.req.url,
133
canonical: `https://material-ui.com${ctx.req.url.replace(/\/$/, '')}/`,
134
styles: (
135
<style
136
id="jss-server-side"
137
// eslint-disable-next-line react/no-danger
138
dangerouslySetInnerHTML={{ __html: css }}
139
/>
140
),
141
};
142
};
143
144
export default MyDocument;