11/* eslint-disable react/no-danger */
22import React from 'react' ;
33import Document , { Head , Html , Main , NextScript } from 'next/document' ;
4+ import PropTypes from 'prop-types' ;
45
56import createEmotionServer from '@emotion/server/create-instance' ;
67import createCache from '@emotion/cache' ;
78
8- function MyDocument ( ) {
9+ const propTypes = {
10+ styles : PropTypes . arrayOf (
11+ PropTypes . string || PropTypes . number || PropTypes . ReactElementLike || React . ReactFragment ,
12+ ) . isRequired ,
13+ } ;
14+
15+ function MyDocument ( { styles } ) {
916 return (
1017 < Html lang = "en" style = { { height : '100%' } } >
1118 < Head >
@@ -62,6 +69,8 @@ function MyDocument() {
6269 ` ,
6370 } }
6471 />
72+ { /* Inject styles first to match with the prepend: true configuration. */ }
73+ { styles }
6574 </ Head >
6675 < body >
6776 < Main />
@@ -92,12 +101,12 @@ MyDocument.getInitialProps = async (ctx) => {
92101 const initialProps = await Document . getInitialProps ( ctx ) ;
93102 // This is important. It prevents emotion to render invalid HTML.
94103 // See https://github.com/mui-org/material-ui/issues/26561#issuecomment-855286153
95- const emotionStyles = extractCriticalToChunks ( initialProps . html ) ;
96- const emotionStyleTags = emotionStyles . styles . map ( ( style ) => (
104+ const chunks = extractCriticalToChunks ( initialProps . html ) ;
105+
106+ const emotionStyleTags = chunks . styles . map ( ( style ) => (
97107 < style
98108 data-emotion = { `${ style . key } ${ style . ids . join ( ' ' ) } ` }
99109 key = { style . key }
100- // eslint-disable-next-line react/no-danger
101110 dangerouslySetInnerHTML = { { __html : style . css } }
102111 />
103112 ) ) ;
@@ -109,4 +118,6 @@ MyDocument.getInitialProps = async (ctx) => {
109118 } ;
110119} ;
111120
121+ MyDocument . propTypes = propTypes ;
122+
112123export default MyDocument ;
0 commit comments