|
|
@ -1,34 +1,52 @@ |
|
|
|
import Document, { DocumentInitialProps, DocumentContext } from 'next/document'; |
|
|
|
import Document, { DocumentInitialProps, DocumentContext } from 'next/document'; |
|
|
|
import { ServerStyleSheets as MaterialUiServerStyleSheets } from '@material-ui/styles'; |
|
|
|
import createEmotionServer from '@emotion/server/create-instance'; |
|
|
|
// import { ServerStyleSheet as StyledComponentSheets } from 'styled-components';
|
|
|
|
|
|
|
|
|
|
|
|
import createEmotionCache from '../lib/create_emotion_cache/createEmotionCache'; |
|
|
|
|
|
|
|
|
|
|
|
class MyDocument extends Document { |
|
|
|
class MyDocument extends Document { |
|
|
|
static async getInitialProps( |
|
|
|
static async getInitialProps( |
|
|
|
ctx: DocumentContext, |
|
|
|
ctx: DocumentContext, |
|
|
|
): Promise<DocumentInitialProps> { |
|
|
|
): Promise<DocumentInitialProps> { |
|
|
|
const materialUiSheets = new MaterialUiServerStyleSheets(); |
|
|
|
|
|
|
|
const originalRenderPage = ctx.renderPage; |
|
|
|
const originalRenderPage = ctx.renderPage; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const emotionCache = createEmotionCache(); |
|
|
|
|
|
|
|
const { extractCriticalToChunks } = createEmotionServer(emotionCache); |
|
|
|
|
|
|
|
|
|
|
|
ctx.renderPage = () => |
|
|
|
ctx.renderPage = () => |
|
|
|
originalRenderPage({ |
|
|
|
originalRenderPage({ |
|
|
|
/* eslint-disable react/display-name */ |
|
|
|
// Temporary; the implicit type of App doesn't include the prop "emotionCache" thus typescript will complain.
|
|
|
|
enhanceApp: (App) => (props) => |
|
|
|
// Find a way to extend the implicit type to add the cache property.
|
|
|
|
materialUiSheets.collect( |
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
<App |
|
|
|
enhanceApp: (App: any) => |
|
|
|
/* eslint-disable react/jsx-props-no-spreading */ |
|
|
|
function EnhanceApp(props) { |
|
|
|
{...props} |
|
|
|
return ( |
|
|
|
/>, |
|
|
|
<App |
|
|
|
), |
|
|
|
emotionCache={emotionCache} |
|
|
|
|
|
|
|
/* eslint-disable react/jsx-props-no-spreading */ |
|
|
|
|
|
|
|
{...props} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const initialProps = await Document.getInitialProps(ctx); |
|
|
|
const initialProps = await Document.getInitialProps(ctx); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const emotionStyles = extractCriticalToChunks(initialProps.html); |
|
|
|
|
|
|
|
const emotionStyleTags = emotionStyles.styles.map((style) => ( |
|
|
|
|
|
|
|
<style |
|
|
|
|
|
|
|
data-emotion={`${style.key} ${style.ids.join(' ')}`} |
|
|
|
|
|
|
|
key={style.key} |
|
|
|
|
|
|
|
// eslint-disable-next-line react/no-danger
|
|
|
|
|
|
|
|
dangerouslySetInnerHTML={{ __html: style.css }} |
|
|
|
|
|
|
|
/> |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
...initialProps, |
|
|
|
...initialProps, |
|
|
|
styles: ( |
|
|
|
styles: ( |
|
|
|
<> |
|
|
|
<> |
|
|
|
{initialProps.styles} |
|
|
|
{initialProps.styles} |
|
|
|
{materialUiSheets.getStyleElement()} |
|
|
|
{emotionStyleTags} |
|
|
|
</> |
|
|
|
</> |
|
|
|
), |
|
|
|
), |
|
|
|
}; |
|
|
|
}; |
|
|
|