Created
June 7, 2021 08:50
-
-
Save DopamineDriven/06f5aca7ffff9282ccb80be74425b4a5 to your computer and use it in GitHub Desktop.
Document.tsx full
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Document, { | |
Head, | |
Html, | |
Main, | |
NextScript, | |
DocumentContext, | |
DocumentProps, | |
DocumentInitialProps | |
} from 'next/document'; | |
import { GA_TRACKING_ID } from '@/lib/analytics'; | |
export default class FadeDocument extends Document< | |
DocumentProps | unknown | |
> { | |
static async getInitialProps( | |
ctx: DocumentContext | |
): Promise<DocumentInitialProps> { | |
const originalRenderPage = ctx.renderPage; | |
const initialProps = await Document.getInitialProps(ctx); | |
try { | |
ctx.renderPage = () => | |
originalRenderPage({ | |
enhanceApp: App => props => <App {...props} /> | |
}); | |
} catch (error) { | |
console.log(error); | |
} | |
return { | |
...initialProps, | |
styles: <>{initialProps.styles}</> | |
}; | |
} | |
render() { | |
return ( | |
<Html lang='en-US'> | |
<Head> | |
<meta charSet='utf-8' /> | |
<link | |
rel='stylesheet' | |
href='https://rsms.me/inter/inter.css' | |
/> | |
<link rel='shortcut icon' href='/meta/favicon.ico' /> | |
<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}', { | |
page: window.location.pathname | |
});` | |
}} | |
/> | |
</Head> | |
<body className='loading'> | |
<Main /> | |
<NextScript /> | |
</body> | |
</Html> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment