-
Notifications
You must be signed in to change notification settings - Fork 30
/
_document.js
50 lines (47 loc) · 1.51 KB
/
_document.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// @flow
import * as React from "react";
import Document, { Head, Html, Main, NextScript } from "next/document";
import styled, { ServerStyleSheet } from "styled-components";
import { GA_TRACKING_ID } from '../gtag'
import type { Context } from "next";
type DocumentContext = Context & { renderPage: Function };
export default class MyDocument extends Document {
static getInitialProps({ renderPage }: DocumentContext) {
const sheet = new ServerStyleSheet();
const page = renderPage(App => props =>
sheet.collectStyles(<App {...props} />)
);
const styleTags = sheet.getStyleElement();
return { ...page, styleTags };
}
render() {
return (
<Html lang="en">
<Head>
{this.props.styleTags}
{/* Global Site Tag (gtag.js) - Google Analytics */}
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
/>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_TRACKING_ID}');
`}}
/>
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}