Files
personal-website/pages/_document.tsx
T
2021-09-05 18:47:45 +01:00

103 lines
2.5 KiB
TypeScript

import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
class Doc extends Document {
static async getInitialProps(ctx: any) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App: any) => (props: any) => 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 async src='https://www.googletagmanager.com/gtag/js?id=G-FMD81GLKS3'></script>
<script
dangerouslySetInnerHTML={{
__html: `
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-FMD81GLKS3');
`
}}
/>
<script
dangerouslySetInnerHTML={{
__html: `
function getInitialTheme() {
const persistedColorPreference = window.localStorage.getItem('theme');
const hasPersistedPreference = typeof persistedColorPreference === 'string';
if (hasPersistedPreference) {
return persistedColorPreference;
}
const mql = window.matchMedia('(prefers-color-scheme: dark)');
const hasMediaQueryPreference = typeof mql.matches === 'boolean';
if (hasMediaQueryPreference) {
return mql.matches ? 'dark' : 'light';
}
return 'light';
}
(() => {
const theme = getInitialTheme();
const root = document.documentElement;
root.style.setProperty('--theme', theme);
root.style.setProperty(
'--background',
theme === 'light' ? '#F9F9F9' : '#262626'
);
root.style.setProperty(
'--secondary-background',
theme === 'light' ? 'white' : '#2F2F2F'
);
root.style.setProperty(
'--text',
theme === 'light' ? 'black' : 'white'
);
root.style.setProperty(
'--text-inverted',
theme === 'light' ? 'white' : 'black'
);
})();
`
}}
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
export default Doc;