Skip to content

Instantly share code, notes, and snippets.

@rulyotano
Created September 1, 2022 10:16
Show Gist options
  • Save rulyotano/3dfc6cdf7b03490bf5d0f78b89d59793 to your computer and use it in GitHub Desktop.
Save rulyotano/3dfc6cdf7b03490bf5d0f78b89d59793 to your computer and use it in GitHub Desktop.
NextJs file for Medium article
import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import { ServerStyleSheets } from '@material-ui/core/styles';
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>
);
}
}
MyDocument.getInitialProps = async (ctx) => {
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()],
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment