bbgo_origin/apps/frontend/pages/_document.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-01-24 06:32:52 +00:00
/* eslint-disable react/jsx-filename-extension */
import React from 'react';
2022-06-11 00:57:54 +00:00
import Document, { Html, Head, Main, NextScript } from 'next/document';
2022-06-12 15:08:25 +00:00
import { ServerStyleSheets } from '@mui/styles';
2021-01-24 06:32:52 +00:00
import theme from '../src/theme';
export default class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
MyDocument.getInitialProps = async (ctx) => {
// Resolution order
//
// On the server:
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. document.getInitialProps
// 4. app.render
// 5. page.render
// 6. document.render
//
// On the server with error:
// 1. document.getInitialProps
// 2. app.render
// 3. page.render
// 4. document.render
//
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render
// 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.
2022-06-11 00:57:54 +00:00
styles: [
...React.Children.toArray(initialProps.styles),
sheets.getStyleElement(),
],
2021-01-24 06:32:52 +00:00
};
};