| 6 | |
| 7 | export default class MyDocument extends Document { |
| 8 | static async getInitialProps(ctx: DocumentContext) { |
| 9 | const sheet = new ServerStyleSheet() |
| 10 | const originalRenderPage = ctx.renderPage |
| 11 | |
| 12 | try { |
| 13 | ctx.renderPage = () => |
| 14 | originalRenderPage({ |
| 15 | enhanceApp: (App) => (props) => |
| 16 | sheet.collectStyles(<App {...props} />), |
| 17 | }) |
| 18 | |
| 19 | const initialProps = await Document.getInitialProps(ctx) |
| 20 | return { |
| 21 | ...initialProps, |
| 22 | styles: ( |
| 23 | <> |
| 24 | {initialProps.styles} |
| 25 | {sheet.getStyleElement()} |
| 26 | </> |
| 27 | ), |
| 28 | } |
| 29 | } finally { |
| 30 | sheet.seal() |
| 31 | } |
| 32 | } |
| 33 | } |