Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

_document.tsx 1.2 KB

You have to be logged in to leave a comment. Sign In
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
47
48
49
50
  1. import Document, { DocumentContext, Html, Head, Main, NextScript } from 'next/document'
  2. import { ServerStyleSheet } from 'styled-components'
  3. import { getThemeProps } from 'components/lib/getThemeProps'
  4. export default class MyDocument extends Document {
  5. static async getInitialProps(ctx: DocumentContext) {
  6. const sheet = new ServerStyleSheet()
  7. const originalRenderPage = ctx.renderPage
  8. try {
  9. ctx.renderPage = () =>
  10. originalRenderPage({
  11. enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
  12. })
  13. const initialProps = await Document.getInitialProps(ctx)
  14. return {
  15. ...initialProps,
  16. cssThemeProps: getThemeProps(ctx.req, 'css'),
  17. styles: (
  18. <>
  19. {initialProps.styles}
  20. {sheet.getStyleElement()}
  21. </>
  22. ),
  23. }
  24. } finally {
  25. sheet.seal()
  26. }
  27. }
  28. render() {
  29. const { colorMode, nightScheme, dayScheme } = (this.props as any).cssThemeProps
  30. return (
  31. <Html>
  32. <Head />
  33. <body
  34. data-color-mode={colorMode}
  35. data-dark-theme={nightScheme}
  36. data-light-theme={dayScheme}
  37. >
  38. <Main />
  39. <NextScript />
  40. </body>
  41. </Html>
  42. )
  43. }
  44. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...