Skip to content

Commit 5fd2f68

Browse files
committed
rewrite including antd css in nextjs app so works with upgraded rspack; upgrade antd
1 parent 0c4e1a3 commit 5fd2f68

File tree

4 files changed

+91
-122
lines changed

4 files changed

+91
-122
lines changed

src/packages/frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"@xterm/addon-webgl": "^0.18.0",
7272
"@xterm/xterm": "^5.5.0",
7373
"anser": "^2.1.1",
74-
"antd": "^5.24.7",
74+
"antd": "^5.27.0",
7575
"antd-img-crop": "^4.21.0",
7676
"async": "^2.6.3",
7777
"audio-extensions": "^0.0.0",

src/packages/next/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"homepage": "https://github.com/sagemathinc/cocalc/tree/master/src/packages/next",
5858
"private": false,
5959
"dependencies": {
60+
"@ant-design/cssinjs": "^1.24.0",
6061
"@ant-design/icons": "^6.0.0",
6162
"@ant-design/v5-patch-for-react-19": "^1.0.3",
6263
"@cocalc/backend": "workspace:*",
@@ -67,7 +68,7 @@
6768
"@cocalc/util": "workspace:*",
6869
"@openapitools/openapi-generator-cli": "^2.19.1",
6970
"@vscode/vscode-languagedetection": "^1.0.22",
70-
"antd": "^5.24.7",
71+
"antd": "^5.27.0",
7172
"antd-img-crop": "^4.21.0",
7273
"awaiting": "^3.0.0",
7374
"base-64": "^1.0.0",
@@ -95,7 +96,6 @@
9596
"zod": "^3.23.5"
9697
},
9798
"devDependencies": {
98-
"@ant-design/cssinjs": "^1.23.0",
9999
"@testing-library/jest-dom": "^6.6.3",
100100
"@testing-library/react": "^16.3.0",
101101
"@types/express": "^4.17.21",

src/packages/next/pages/_document.tsx

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,50 @@
1-
/*
2-
This custom document is needed to workaround this bug in antd + nextjs:
3-
4-
https://github.com/ant-design/ant-design/issues/38767
5-
6-
The actual fix -- i.e., this entire file -- comes from
7-
8-
https://github.com/ant-design/ant-design/issues/38767#issuecomment-1350362026
9-
10-
which is for a different bug in antd + nextjs, but it happens to fix
11-
the same problem, and fortunately also works with the older nextjs 12.x, which
12-
we are currently stuck with.
13-
14-
See also the discussion at https://github.com/ant-design/ant-design/issues/39891
15-
*/
16-
1+
// pages/_document.tsx
172
import type { DocumentContext, DocumentInitialProps } from "next/document";
183
import Document, { Head, Html, Main, NextScript } from "next/document";
19-
204
import { createCache, extractStyle, StyleProvider } from "@ant-design/cssinjs";
21-
225
import { Locale } from "@cocalc/util/i18n";
23-
246
import { query2locale } from "locales/misc";
257

26-
export default class MyDocument extends Document {
27-
static async getInitialProps(ctx: DocumentContext): Promise<
28-
DocumentInitialProps & {
29-
locale: Locale;
30-
}
31-
> {
8+
export default class MyDocument extends Document<{ locale: Locale }> {
9+
static async getInitialProps(
10+
ctx: DocumentContext,
11+
): Promise<DocumentInitialProps & { locale: Locale }> {
3212
const locale = query2locale(ctx.query);
33-
3413
const cache = createCache();
3514
const originalRenderPage = ctx.renderPage;
3615

37-
// The IntlProvider is only for english and all components with translations in the frontend
3816
ctx.renderPage = () =>
3917
originalRenderPage({
40-
enhanceApp: (App) => (props) =>
41-
(
42-
<StyleProvider cache={cache}>
43-
<App {...props} {...{ locale }} />
44-
</StyleProvider>
45-
),
18+
enhanceApp: (App: any) => (props: any) => (
19+
<StyleProvider cache={cache} hashPriority="high">
20+
<App {...props} locale={locale} />
21+
</StyleProvider>
22+
),
4623
});
4724

4825
const initialProps = await Document.getInitialProps(ctx);
4926

27+
// inline critical AntD CSS as real <style> tags (no script hack)
28+
const css = extractStyle(cache, { plain: true, types: ["style", "token"] });
29+
5030
return {
5131
...initialProps,
5232
locale,
5333
styles: (
5434
<>
5535
{initialProps.styles}
56-
{/* This is hack, `extractStyle` does not currently support returning JSX or related data. */}
57-
<script
58-
dangerouslySetInnerHTML={{
59-
__html: `</script>${extractStyle(cache)}<script>`,
60-
}}
36+
<style
37+
// keep it obvious for debugging
38+
data-antd="cssinjs-ssr"
39+
// extractStyle returns complete <style> tags; that’s OK here
40+
// If you prefer only the CSS text, you can parse it, but this works well in practice.
41+
dangerouslySetInnerHTML={{ __html: css }}
6142
/>
6243
</>
6344
),
6445
};
6546
}
6647

67-
// TODO: this "lang={...}" is only working for the very first page that's being loaded
68-
// next's dynamic page updates to not have an impact on this. So, to really fix this, we
69-
// probably have to get rid of this _document customization and update to version 15 properly.
7048
render() {
7149
return (
7250
<Html lang={this.props.locale}>

0 commit comments

Comments
 (0)