Skip to content

Commit 40273b2

Browse files
committed
edit to MyDocument, 9-end
1 parent 7291b7f commit 40273b2

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

book/9-end-functional/pages/_document.jsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
/* eslint-disable react/no-danger */
22
import React from 'react';
33
import Document, { Head, Html, Main, NextScript } from 'next/document';
4+
import PropTypes from 'prop-types';
45

56
import createEmotionServer from '@emotion/server/create-instance';
67
import 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+
112123
export default MyDocument;

book/9-end/pages/_document.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
/* eslint-disable react/no-danger */
22
import React from 'react';
33
import Document, { Head, Html, Main, NextScript } from 'next/document';
4+
import PropTypes from 'prop-types';
45

56
import createEmotionServer from '@emotion/server/create-instance';
67
import createCache from '@emotion/cache';
78

9+
const propTypes = {
10+
styles: PropTypes.arrayOf(
11+
PropTypes.string || PropTypes.number || PropTypes.ReactElementLike || React.ReactFragment,
12+
).isRequired,
13+
};
14+
815
class MyDocument extends Document {
916
static getInitialProps = async (ctx) => {
1017
// Render app and page and get the context of the page with collected side effects.
@@ -27,8 +34,9 @@ class MyDocument extends Document {
2734
const initialProps = await Document.getInitialProps(ctx);
2835
// This is important. It prevents emotion to render invalid HTML.
2936
// See https://github.com/mui-org/material-ui/issues/26561#issuecomment-855286153
30-
const emotionStyles = extractCriticalToChunks(initialProps.html);
31-
const emotionStyleTags = emotionStyles.styles.map((style) => (
37+
const chunks = extractCriticalToChunks(initialProps.html);
38+
39+
const emotionStyleTags = chunks.styles.map((style) => (
3240
<style
3341
data-emotion={`${style.key} ${style.ids.join(' ')}`}
3442
key={style.key}
@@ -105,6 +113,8 @@ class MyDocument extends Document {
105113
`,
106114
}}
107115
/>
116+
{/* Inject styles first to match with the prepend: true configuration. */}
117+
{this.props.styles}
108118
</Head>
109119
<body>
110120
<Main />
@@ -115,4 +125,6 @@ class MyDocument extends Document {
115125
}
116126
}
117127

128+
MyDocument.propTypes = propTypes;
129+
118130
export default MyDocument;

0 commit comments

Comments
 (0)