Permalink
material-ui/pages/_document.js
Newer
100644
124 lines (114 sloc)
4.05 KB
1
import React from 'react';
2
import Document, { Head, Main, NextScript } from 'next/document';
3
import getPageContext from 'docs/src/modules/styles/getPageContext';
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
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" />
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" />
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');
72
<script defer src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js" />
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
// Get the context of the page to collected side effects.
98
const pageContext = getPageContext();
99
const page = ctx.renderPage(Component => props => (
104
if (process.env.NODE_ENV === 'production') {
105
const result1 = await prefixer.process(css, { from: undefined });
113
canonical: `https://material-ui.com${ctx.req.url.replace(/\/$/, '')}/`,
114
styles: (
115
<style
116
id="jss-server-side"
117
// eslint-disable-next-line react/no-danger
118
dangerouslySetInnerHTML={{ __html: css }}
119
/>
120
),
121
};
122
};
123
124
export default MyDocument;