-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.tsx
More file actions
101 lines (88 loc) · 2.75 KB
/
document.tsx
File metadata and controls
101 lines (88 loc) · 2.75 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import Document, {
Html,
Head,
Main,
NextScript,
DocumentContext,
DocumentInitialProps,
} from 'next/document'
import { ServerStyleSheet } from 'styled-components'
import { langs, defaultLang } from 'core/texts'
const GA_ID = process.env.NEXT_PUBLIC_GA_ID
const baseUrl = process.env.NEXT_PUBLIC_SITE_BASEURL
type MyDocumentProps = DocumentInitialProps & {
origin: string
styles: JSX.Element
}
export default class MyDocument extends Document<MyDocumentProps> {
static async getInitialProps(ctx: DocumentContext) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
origin: baseUrl,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
render() {
return (
<Html>
<Head>
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<link rel="shortcut icon" href="/favicon.ico" />
<meta name="theme-color" content="#ffffff" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;900&display=swap"
rel="stylesheet"
/>
<script async src={`https://www.googletagmanager.com/gtag/js?id=${GA_ID}`} />
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GA_ID}', { page_path: window.location.pathname });
`,
}}
/>
<link rel="canonical" href={this.props.origin.replace(/\/$/, '')} />
{/* See: https://developers.google.com/search/docs/advanced/crawling/localized-versions#html */}
<link
rel="alternate"
hrefLang="x-default"
href={`${this.props.origin.replace(/\/$/, '')}/`}
/>
{langs.map((lang) => (
<link
key={lang}
rel="alternate"
hrefLang={lang}
href={`${this.props.origin.replace(/\/$/, '')}/${lang === defaultLang ? '' : lang}`}
/>
))}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}