import React from 'react'; import type { DocumentContext } from 'next/document'; import Document, { Html, Head, Main, NextScript } from 'next/document'; import createEmotionServer from '@emotion/server/create-instance'; import { ServerStyleSheet } from 'styled-components'; import createEmotionCache from '../utils/createEmotionCache'; export default class MyDocument extends Document { static async getInitialProps(ctx: DocumentContext) { const styledComponentsSheet = new ServerStyleSheet(); const originalRenderPage = ctx.renderPage; const cache = createEmotionCache(); const { extractCriticalToChunks } = createEmotionServer(cache); try { ctx.renderPage = () => originalRenderPage({ // eslint-disable-next-line @typescript-eslint/no-explicit-any enhanceApp: (App: any) => (props: any) => styledComponentsSheet.collectStyles( ), }); const initialProps = await Document.getInitialProps(ctx); const emotionStyles = extractCriticalToChunks(initialProps.html); const emotionStyleTags = emotionStyles.styles.map((style) => (