Skip to content

Commit 2d252b7

Browse files
committed
edit in MyDocument, 1-end
1 parent 40273b2 commit 2d252b7

File tree

1 file changed

+12
-47
lines changed

1 file changed

+12
-47
lines changed

book/1-end/pages/_document.jsx

Lines changed: 12 additions & 47 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}
@@ -96,6 +104,7 @@ class MyDocument extends Document {
96104
}
97105
`}
98106
</style>
107+
{this.props.styles}
99108
</Head>
100109
<body>
101110
<Main />
@@ -106,50 +115,6 @@ class MyDocument extends Document {
106115
}
107116
}
108117

109-
// MyDocument.getInitialProps = async (ctx) => {
110-
// // Resolution order
111-
// //
112-
// // On the server:
113-
// // 1. app.getInitialProps
114-
// // 2. page.getInitialProps
115-
// // 3. document.getInitialProps
116-
// // 4. app.render
117-
// // 5. page.render
118-
// // 6. document.render
119-
// //
120-
// // On the server with error:
121-
// // 1. document.getInitialProps
122-
// // 2. app.render
123-
// // 3. page.render
124-
// // 4. document.render
125-
// //
126-
// // On the client
127-
// // 1. app.getInitialProps
128-
// // 2. page.getInitialProps
129-
// // 3. app.render
130-
// // 4. page.render
131-
132-
// // Render app and page and get the context of the page with collected side effects.
133-
// const sheets = new ServerStyleSheets();
134-
// const originalRenderPage = ctx.renderPage;
135-
136-
// ctx.renderPage = () =>
137-
// originalRenderPage({
138-
// enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
139-
// });
140-
141-
// const initialProps = await Document.getInitialProps(ctx);
142-
143-
// return {
144-
// ...initialProps,
145-
// // Styles fragment is rendered after the app and page rendering finish.
146-
// styles: (
147-
// <React.Fragment>
148-
// {initialProps.styles}
149-
// {sheets.getStyleElement()}
150-
// </React.Fragment>
151-
// ),
152-
// };
153-
// };
118+
MyDocument.propTypes = propTypes;
154119

155120
export default MyDocument;

0 commit comments

Comments
 (0)