2022-12-02 10:55:56 +00:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
|
|
|
import * as React from "react";
|
|
|
|
|
import Document, { Html, Head, Main, NextScript } from "next/document";
|
|
|
|
|
import createEmotionServer from "@emotion/server/create-instance";
|
|
|
|
|
import createEmotionCache from "lib/createEmotionCache";
|
|
|
|
|
|
|
|
|
|
export default class LinkDocument extends Document {
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<Html lang="en">
|
|
|
|
|
<Head />
|
|
|
|
|
<body>
|
|
|
|
|
<Main />
|
|
|
|
|
<NextScript />
|
|
|
|
|
</body>
|
|
|
|
|
</Html>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinkDocument.getInitialProps = async (ctx) => {
|
|
|
|
|
const originalRenderPage = ctx.renderPage;
|
|
|
|
|
const cache = createEmotionCache();
|
|
|
|
|
const { extractCriticalToChunks } = createEmotionServer(cache as any);
|
|
|
|
|
|
|
|
|
|
ctx.renderPage = () =>
|
|
|
|
|
originalRenderPage({
|
2023-03-14 17:40:24 +00:00
|
|
|
enhanceApp: (App: any) => (props: any) =>
|
2022-12-02 10:55:56 +00:00
|
|
|
<App emotionCache={cache} {...props} />,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
...initialProps,
|
|
|
|
|
styles: [
|
|
|
|
|
...React.Children.toArray(initialProps.styles),
|
|
|
|
|
...emotionStyleTags,
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
};
|