Skip to content

Commit 9fc33fd

Browse files
authored
fix: style missing with loading (#14)
* fix: style missing with loading * chore: code clean * fix: filter empty styles
1 parent 774964f commit 9fc33fd

File tree

5 files changed

+67
-49
lines changed

5 files changed

+67
-49
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function Loading() {
2+
// You can add any UI inside Loading, including a Skeleton.
3+
return <div>Loading...</div>;
4+
}

example/with-app-router/app/page.tsx

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,65 @@ type FieldType = {
1717
remember?: string;
1818
};
1919

20-
const Page: React.FC = () => (
21-
<ConfigProvider theme={{ cssVar: true }}>
22-
<Form
23-
name="basic"
24-
labelCol={{ span: 8 }}
25-
wrapperCol={{ span: 16 }}
26-
style={{ maxWidth: 600 }}
27-
initialValues={{ remember: true }}
28-
onFinish={onFinish}
29-
onFinishFailed={onFinishFailed}
30-
autoComplete="off"
31-
>
32-
<Form.Item<FieldType>
33-
label="Username"
34-
name="username"
35-
rules={[{ required: true, message: 'Please input your username!' }]}
36-
>
37-
<Input />
38-
</Form.Item>
20+
let promise;
21+
function genPromise() {
22+
if (promise) return promise;
23+
promise = new Promise((resolve) => {
24+
setTimeout(() => {
25+
promise = null;
26+
resolve({});
27+
}, 3 * 1000);
28+
});
29+
return promise;
30+
}
3931

40-
<Form.Item<FieldType>
41-
label="Password"
42-
name="password"
43-
rules={[{ required: true, message: 'Please input your password!' }]}
44-
>
45-
<Input.Password />
46-
</Form.Item>
32+
const Page: React.FC = () => {
33+
React.use(genPromise());
4734

48-
<Form.Item<FieldType>
49-
name="remember"
50-
valuePropName="checked"
51-
wrapperCol={{ offset: 8, span: 16 }}
35+
return (
36+
<ConfigProvider theme={{ cssVar: true }}>
37+
<Form
38+
name="basic"
39+
labelCol={{ span: 8 }}
40+
wrapperCol={{ span: 16 }}
41+
style={{ maxWidth: 600 }}
42+
initialValues={{ remember: true }}
43+
onFinish={onFinish}
44+
onFinishFailed={onFinishFailed}
45+
autoComplete="off"
5246
>
53-
<Checkbox>Remember me</Checkbox>
54-
</Form.Item>
47+
<Form.Item<FieldType>
48+
label="Username"
49+
name="username"
50+
rules={[{ required: true, message: 'Please input your username!' }]}
51+
>
52+
<Input />
53+
</Form.Item>
54+
55+
<Form.Item<FieldType>
56+
label="Password"
57+
name="password"
58+
rules={[{ required: true, message: 'Please input your password!' }]}
59+
>
60+
<Input.Password />
61+
</Form.Item>
5562

56-
<Form.Item wrapperCol={{ offset: 8, span: 16 }}>
57-
<Button type="primary" htmlType="submit">
58-
Submit
59-
</Button>
60-
</Form.Item>
61-
</Form>
62-
</ConfigProvider>
63-
);
63+
<Form.Item<FieldType>
64+
name="remember"
65+
valuePropName="checked"
66+
wrapperCol={{ offset: 8, span: 16 }}
67+
>
68+
<Checkbox>Remember me</Checkbox>
69+
</Form.Item>
70+
71+
<Form.Item wrapperCol={{ offset: 8, span: 16 }}>
72+
<Button type="primary" htmlType="submit">
73+
Submit
74+
</Button>
75+
</Form.Item>
76+
</Form>
77+
</ConfigProvider>
78+
);
79+
};
6480

6581
export default Page;

example/with-app-router/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
5+
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"prettier": "prettier --write \"**/*.{ts,tsx,js,jsx,json,md}\""
3434
},
3535
"devDependencies": {
36-
"@ant-design/cssinjs": "^1.18.2",
36+
"@ant-design/cssinjs": "^1.24.0",
3737
"@types/react": "^18.2.45",
3838
"@types/react-dom": "^18.2.18",
3939
"@umijs/fabric": "^2.0.8",
@@ -50,7 +50,7 @@
5050
"typescript": "^5.3.3"
5151
},
5252
"peerDependencies": {
53-
"@ant-design/cssinjs": "^1.18.2",
53+
"@ant-design/cssinjs": "^1.24.0",
5454
"antd": "^5.0.0",
5555
"next": "^14.0.0 || ^15.0.0",
5656
"react": ">=16.0.0",

src/AntdRegistry.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
'use client';
22

3-
import React, { type FC, useRef, useState } from 'react';
4-
import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs';
3+
import React, { type FC, useState } from 'react';
54
import type { StyleProviderProps } from '@ant-design/cssinjs';
5+
import { createCache, extractStyle, StyleProvider } from '@ant-design/cssinjs';
66
import { useServerInsertedHTML } from 'next/navigation';
77

88
type AntdRegistryProps = Omit<StyleProviderProps, 'cache'>;
99

1010
const AntdRegistry: FC<AntdRegistryProps> = (props) => {
1111
const [cache] = useState(() => createCache());
12-
const inserted = useRef(false);
1312

1413
useServerInsertedHTML(() => {
15-
const styleText = extractStyle(cache, { plain: true });
14+
const styleText = extractStyle(cache, { plain: true, once: true });
1615

17-
if (inserted.current) {
16+
if (styleText.includes('.data-ant-cssinjs-cache-path{content:"";}')) {
1817
return null;
1918
}
20-
inserted.current = true;
2119

2220
return (
2321
<style

0 commit comments

Comments
 (0)