Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/next/pages/_document.tsx
Views: 687
/*1This custom document is needed to workaround this bug in antd + nextjs:23https://github.com/ant-design/ant-design/issues/3876745The actual fix -- i.e., this entire file -- comes from67https://github.com/ant-design/ant-design/issues/38767#issuecomment-135036202689which is for a different bug in antd + nextjs, but it happens to fix10the same problem, and fortunately also works with the older nextjs 12.x, which11we are currently stuck with.1213See also the discussion at https://github.com/ant-design/ant-design/issues/3989114*/1516import type { DocumentContext, DocumentInitialProps } from "next/document";17import Document, { Html, Head, Main, NextScript } from "next/document";18import { createCache, extractStyle, StyleProvider } from "@ant-design/cssinjs";1920export default class MyDocument extends Document {21static async getInitialProps(22ctx: DocumentContext23): Promise<DocumentInitialProps> {24const cache = createCache();25const originalRenderPage = ctx.renderPage;2627ctx.renderPage = () =>28originalRenderPage({29enhanceApp: (App) => (props) =>30(31<StyleProvider cache={cache}>32<App {...props} />33</StyleProvider>34),35});3637const initialProps = await Document.getInitialProps(ctx);3839return {40...initialProps,41styles: (42<>43{initialProps.styles}44{/* This is hack, `extractStyle` does not currently support returning JSX or related data. */}45<script46dangerouslySetInnerHTML={{47__html: `</script>${extractStyle(cache)}<script>`,48}}49/>50</>51),52};53}54render() {55return (56<Html>57<Head />58<body>59<Main />60<NextScript />61</body>62</Html>63);64}65}666768