mirror of
https://github.com/hazemKrimi/personal-website.git
synced 2026-05-01 18:00:26 +00:00
71 lines
1.5 KiB
TypeScript
71 lines
1.5 KiB
TypeScript
import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document';
|
|
import Script from 'next/script';
|
|
|
|
import { ServerStyleSheet } from 'styled-components';
|
|
|
|
import { GOOGLE_ANALYTICS_KEY } from '../utils/gtag';
|
|
|
|
class Doc extends Document {
|
|
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,
|
|
styles: (
|
|
<>
|
|
{initialProps.styles}
|
|
{sheet.getStyleElement()}
|
|
</>
|
|
)
|
|
};
|
|
} finally {
|
|
sheet.seal();
|
|
}
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<Html>
|
|
<Head>
|
|
<Script
|
|
strategy='afterInteractive'
|
|
src={`https://www.googletagmanager.com/gtag/js?id=${GOOGLE_ANALYTICS_KEY}`}
|
|
/>
|
|
<Script
|
|
id='google-analytics'
|
|
strategy='afterInteractive'
|
|
dangerouslySetInnerHTML={{
|
|
__html: `
|
|
window.dataLayer = window.dataLayer || [];
|
|
|
|
function gtag() {
|
|
dataLayer.push(arguments);
|
|
}
|
|
|
|
gtag('js', new Date());
|
|
gtag('config', ${GOOGLE_ANALYTICS_KEY}, {
|
|
page_path: window.location.pathname,
|
|
});
|
|
`
|
|
}}
|
|
/>
|
|
</Head>
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default Doc;
|