forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_document.tsx
46 lines (40 loc) · 1.2 KB
/
_document.tsx
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
import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document'
import { ServerStyleSheet } from 'styled-components'
import { getThemeProps } from 'components/lib/getThemeProps'
export default class MyDocument 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,
cssThemeProps: getThemeProps(ctx.req, 'css'),
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
render() {
const { colorMode, nightTheme, dayTheme } = (this.props as any).cssThemeProps
return (
<Html>
<Head />
<body data-color-mode={colorMode} data-dark-theme={nightTheme} data-light-theme={dayTheme}>
<Main />
<NextScript />
</body>
</Html>
)
}
}