This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
website/src/pages/_document.tsx

46 lines
1.0 KiB
TypeScript

import React from 'react';
import Document, {
DocumentContext,
Html,
Main,
NextScript,
Head,
} from 'next/document';
import { ServerStyleSheets } from '@material-ui/styles';
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheets.collect(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [
...React.Children.toArray(initialProps.styles),
sheets.getStyleElement(),
],
};
}
render() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}