50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import type { DocumentContext, DocumentInitialProps } from "next/document";
|
|
import NextDocument, { Head, Html, Main, NextScript } from "next/document";
|
|
import { ServerStyleSheet } from "styled-components";
|
|
import { DEFAULT_LOCALE } from "utils/constants";
|
|
|
|
const withStyledComponents = async (
|
|
ctx: DocumentContext
|
|
): Promise<DocumentInitialProps> => {
|
|
const { renderPage } = ctx;
|
|
const sheet = new ServerStyleSheet();
|
|
|
|
try {
|
|
ctx.renderPage = () =>
|
|
renderPage({
|
|
enhanceApp: (App) => (props) => sheet.collectStyles(<App {...props} />),
|
|
});
|
|
|
|
const { styles, ...initialProps } = await NextDocument.getInitialProps(ctx);
|
|
|
|
return {
|
|
...initialProps,
|
|
styles: [styles, sheet.getStyleElement()],
|
|
};
|
|
} finally {
|
|
sheet.seal();
|
|
}
|
|
};
|
|
|
|
class Document extends NextDocument {
|
|
public static async getInitialProps(
|
|
ctx: DocumentContext
|
|
): Promise<DocumentInitialProps> {
|
|
return withStyledComponents(ctx);
|
|
}
|
|
|
|
public render(): JSX.Element {
|
|
return (
|
|
<Html lang={DEFAULT_LOCALE}>
|
|
<Head />
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default Document;
|