Path: blob/master/src/packages/next/pages/_document.tsx
1447 views
/*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, { Head, Html, Main, NextScript } from "next/document";1819import { createCache, extractStyle, StyleProvider } from "@ant-design/cssinjs";2021import { Locale } from "@cocalc/util/i18n";2223import { query2locale } from "locales/misc";2425export default class MyDocument extends Document {26static async getInitialProps(ctx: DocumentContext): Promise<27DocumentInitialProps & {28locale: Locale;29}30> {31const locale = query2locale(ctx.query);3233const cache = createCache();34const originalRenderPage = ctx.renderPage;3536// The IntlProvider is only for english and all components with translations in the frontend37ctx.renderPage = () =>38originalRenderPage({39enhanceApp: (App) => (props) =>40(41<StyleProvider cache={cache}>42<App {...props} {...{ locale }} />43</StyleProvider>44),45});4647const initialProps = await Document.getInitialProps(ctx);4849return {50...initialProps,51locale,52styles: (53<>54{initialProps.styles}55{/* This is hack, `extractStyle` does not currently support returning JSX or related data. */}56<script57dangerouslySetInnerHTML={{58__html: `</script>${extractStyle(cache)}<script>`,59}}60/>61</>62),63};64}6566// TODO: this "lang={...}" is only working for the very first page that's being loaded67// next's dynamic page updates to not have an impact on this. So, to really fix this, we68// probably have to get rid of this _document customization and update to version 15 properly.69render() {70return (71<Html lang={this.props.locale}>72<Head />73<body>74<Main />75<NextScript />76</body>77</Html>78);79}80}818283