1+ import React from 'react' ;
2+ import Document , { Html , Head , Main , NextScript } from 'next/document' ;
3+ import { ServerStyleSheets } from '@material-ui/core/styles' ;
4+ import theme from '../lib/theme' ;
5+ export default class MyDocument extends Document {
6+ render ( ) {
7+ return (
8+ < Html lang = "en" >
9+ < Head >
10+ { /* PWA primary color */ }
11+ < meta name = "theme-color" content = { theme . palette . primary . main } />
12+ < link
13+ rel = "stylesheet"
14+ href = "https://fonts.googleapis.com/css?family=Roboto:300,400,500,700& display = swap "
15+ />
16+ </ Head >
117
2- import React from 'react'
3- import NextDocument from 'next/document'
4- import { ServerStyleSheet as StyledComponentSheets } from 'styled-components'
5- import { ServerStyleSheets as MaterialUiServerStyleSheets } from '@material-ui/core/styles'
6- export default class Document extends NextDocument {
7- static async getInitialProps ( ctx ) {
8- const styledComponentSheet = new StyledComponentSheets ( )
9- const materialUiSheets = new MaterialUiServerStyleSheets ( )
10- const originalRenderPage = ctx . renderPage
11- try {
12- ctx . renderPage = ( ) =>
13- originalRenderPage ( {
14- enhanceApp : App => props =>
15- styledComponentSheet . collectStyles (
16- materialUiSheets . collect ( < App { ...props } /> ) ,
17- ) ,
18- } )
19- const initialProps = await NextDocument . getInitialProps ( ctx )
20- return {
21- ...initialProps ,
22- styles : [
23- < React . Fragment key = "styles" >
24- { initialProps . styles }
25- { materialUiSheets . getStyleElement ( ) }
26- { styledComponentSheet . getStyleElement ( ) }
27- </ React . Fragment > ,
28- ] ,
29- }
30- } finally {
31- styledComponentSheet . seal ( )
32- }
18+ < body dir = "rtl" >
19+ < Main />
20+ < NextScript />
21+ </ body >
22+
23+ </ Html >
24+ ) ;
3325 }
34- }
26+ }
27+
28+ // `getInitialProps` belongs to `_document` (instead of `_app`),
29+ // it's compatible with server-side generation (SSG).
30+ MyDocument . getInitialProps = async ( ctx ) => {
31+ // Resolution order
32+ //
33+ // On the server:
34+ // 1. app.getInitialProps
35+ // 2. page.getInitialProps
36+ // 3. document.getInitialProps
37+ // 4. app.render
38+ // 5. page.render
39+ // 6. document.render
40+ //
41+ // On the server with error:
42+ // 1. document.getInitialProps
43+ // 2. app.render
44+ // 3. page.render
45+ // 4. document.render
46+ //
47+ // On the client
48+ // 1. app.getInitialProps
49+ // 2. page.getInitialProps
50+ // 3. app.render
51+ // 4. page.render
52+
53+ // Render app and page and get the context of the page with collected side effects.
54+ const sheets = new ServerStyleSheets ( ) ;
55+ const originalRenderPage = ctx . renderPage ;
56+
57+ ctx . renderPage = ( ) =>
58+ originalRenderPage ( {
59+ enhanceApp : ( App ) => ( props ) => sheets . collect ( < App { ...props } /> ) ,
60+ } ) ;
61+
62+ const initialProps = await Document . getInitialProps ( ctx ) ;
63+
64+ return {
65+ ...initialProps ,
66+ // Styles fragment is rendered after the app and page rendering finish.
67+ styles : [ ...React . Children . toArray ( initialProps . styles ) , sheets . getStyleElement ( ) ] ,
68+ } ;
69+ } ;
0 commit comments