-
Notifications
You must be signed in to change notification settings - Fork 0
/
_document.tsx
44 lines (41 loc) · 1.34 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
import {AppProps} from 'next/app'
import Document, {DocumentContext, DocumentInitialProps} from 'next/document'
import {ServerStyleSheet} from 'styled-components'
import {ServerStyleSheets} from '@material-ui/core/styles'
import {AppType, ComponentsEnhancer, RenderPageResult} from 'next/dist/shared/lib/utils'
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
const sheet = new ServerStyleSheet()
const sheets = new ServerStyleSheets()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = (options?: ComponentsEnhancer): RenderPageResult | Promise<RenderPageResult> => {
return originalRenderPage({
...options,
enhanceApp: (App: AppType): AppType => {
return (props: AppProps): JSX.Element => {
return sheets.collect(
sheet.collectStyles(
<App {...props} />
)
)
}
},
})
}
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheets.getStyleElement()}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
}