Permalink
material-ui/pages/_document.js
Newer
100644
137 lines (123 sloc)
4.34 KB
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.
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
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 name="theme-color" content={pageContext.theme.palette.primary.main} />
50
<link rel="shortcut icon" href="/static/favicon.ico" />
53
{/*
54
Preconnect allows the browser to setup early connections before an HTTP request
55
is actually sent to the server.
56
This includes DNS lookups, TLS negotiations, TCP handshakes.
57
*/}
58
<link href="https://fonts.gstatic.com" rel="preconnect" crossOrigin="anonymous" />
59
<style id="insertion-point-jss" />
64
{/* Global Site Tag (gtag.js) - Google Analytics */}
65
<script defer src="https://www.google-analytics.com/analytics.js" />
66
<script
67
// eslint-disable-next-line react/no-danger
68
dangerouslySetInnerHTML={{
69
__html: `
70
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
71
window.ga('create','${GOOGLE_ID}','auto');
75
<script defer src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js" />
82
MyDocument.getInitialProps = async ctx => {
83
// Resolution order
84
//
85
// On the server:
86
// 1. page.getInitialProps
87
// 2. document.getInitialProps
88
// 3. page.render
89
// 4. document.render
90
//
91
// On the server with error:
92
// 2. document.getInitialProps
93
// 3. page.render
94
// 4. document.render
95
//
96
// On the client
97
// 1. page.getInitialProps
98
// 3. page.render
99
100
// Render app and page and get the context of the page with collected side effects.
101
let pageContext;
102
const page = ctx.renderPage(Component => {
103
const WrappedComponent = props => {
104
pageContext = props.pageContext;
105
return <Component {...props} />;
106
};
107
108
WrappedComponent.propTypes = {
109
pageContext: PropTypes.object.isRequired,
110
};
111
112
return WrappedComponent;
113
});
116
if (process.env.NODE_ENV === 'production') {
117
const result1 = await prefixer.process(css, { from: undefined });
126
canonical: `https://material-ui.com${ctx.req.url.replace(/\/$/, '')}/`,
127
styles: (
128
<style
129
id="jss-server-side"
130
// eslint-disable-next-line react/no-danger
131
dangerouslySetInnerHTML={{ __html: css }}
132
/>
133
),
134
};
135
};
136
137
export default MyDocument;