import Document, { DocumentInitialProps, DocumentContext } from 'next/document'; import createEmotionServer from '@emotion/server/create-instance'; import createEmotionCache from '../lib/create_emotion_cache/createEmotionCache'; class MyDocument extends Document { static async getInitialProps( ctx: DocumentContext, ): Promise { const originalRenderPage = ctx.renderPage; const emotionCache = createEmotionCache(); const { extractCriticalToChunks } = createEmotionServer(emotionCache); ctx.renderPage = () => originalRenderPage({ // Temporary; the implicit type of App doesn't include the prop "emotionCache" thus typescript will complain. // Find a way to extend the implicit type to add the cache property. // eslint-disable-next-line @typescript-eslint/no-explicit-any enhanceApp: (App: any) => function EnhanceApp(props) { return ( ); }, }); const initialProps = await Document.getInitialProps(ctx); const emotionStyles = extractCriticalToChunks(initialProps.html); const emotionStyleTags = emotionStyles.styles.map((style) => (