Path: blob/master/src/packages/next/pages/_document.tsx
5790 views
// pages/_document.tsx1import type { DocumentContext, DocumentInitialProps } from "next/document";2import Document, { Head, Html, Main, NextScript } from "next/document";3import { createCache, extractStyle, StyleProvider } from "@ant-design/cssinjs";4import { Locale } from "@cocalc/util/i18n";5import { query2locale } from "locales/misc";67export default class MyDocument extends Document<{ locale: Locale }> {8static async getInitialProps(9ctx: DocumentContext,10): Promise<DocumentInitialProps & { locale: Locale }> {11const locale = query2locale(ctx.query);12const cache = createCache();13const originalRenderPage = ctx.renderPage;1415ctx.renderPage = () =>16originalRenderPage({17enhanceApp: (App: any) => (props: any) => (18<StyleProvider cache={cache} hashPriority="high">19<App {...props} locale={locale} />20</StyleProvider>21),22});2324const initialProps = await Document.getInitialProps(ctx);2526// inline critical AntD CSS as real <style> tags (no script hack)27const css = extractStyle(cache, { plain: true, types: ["style", "token"] });2829return {30...initialProps,31locale,32styles: (33<>34{initialProps.styles}35<style36// keep it obvious for debugging37data-antd="cssinjs-ssr"38// extractStyle returns complete <style> tags; that’s OK here39// If you prefer only the CSS text, you can parse it, but this works well in practice.40dangerouslySetInnerHTML={{ __html: css }}41/>42</>43),44};45}4647render() {48return (49<Html lang={this.props.locale}>50<Head />51<body>52<Main />53<NextScript />54</body>55</Html>56);57}58}596061