web/pages/_document.tsx (21 lines of code) (raw):

/** * Copyright 2021 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ServerStyleSheets } from "@material-ui/core"; import Document, { DocumentContext, DocumentInitialProps, Head, Html, Main, NextScript, } from "next/document"; import { Children } from "react"; import { enabledGa, GA_MEASUREMENT_ID } from "../src/gtag"; export default class MyDocument extends Document { render(): JSX.Element { return ( <Html lang={this.props.locale}> <Head> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" /> <link rel="icon" href="/favicon.ico" /> {enabledGa ? ( <> <script async src={`https://www.googletagmanager.com/gtag/js?id=${GA_MEASUREMENT_ID}`} /> <script dangerouslySetInnerHTML={{ __html: ` window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '${GA_MEASUREMENT_ID}', { page_path: window.location.pathname, }); `, }} /> </> ) : null} </Head> <body> <Main /> <NextScript /> </body> </Html> ); } } MyDocument.getInitialProps = async ( ctx: DocumentContext ): Promise<DocumentInitialProps> => { const sheets = new ServerStyleSheets(); const originalRenderPage = ctx.renderPage; ctx.renderPage = () => originalRenderPage({ enhanceApp: (App) => (props) => sheets.collect(<App {...props} />), }); const initialProps = await Document.getInitialProps(ctx); return { ...initialProps, styles: [ ...Children.toArray(initialProps.styles), sheets.getStyleElement(), ], }; };