Skip to content

Commit 61e5746

Browse files
authored
fix(StyleContext): fix style cache loaded repeat (#7029)
Co-authored-by: unknown <aibayanyu20>
1 parent 7786a81 commit 61e5746

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

components/_util/cssinjs/StyleContext.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import type { ShallowRef, ExtractPropTypes, InjectionKey, Ref } from 'vue';
2-
import { provide, defineComponent, unref, inject, watch, shallowRef } from 'vue';
2+
import {
3+
provide,
4+
defineComponent,
5+
unref,
6+
inject,
7+
watch,
8+
shallowRef,
9+
getCurrentInstance,
10+
} from 'vue';
311
import CacheEntity from './Cache';
412
import type { Linter } from './linters/interface';
513
import type { Transformer } from './transformers/interface';
@@ -80,14 +88,36 @@ const StyleContextKey: InjectionKey<ShallowRef<Partial<StyleContextProps>>> =
8088
Symbol('StyleContextKey');
8189

8290
export type UseStyleProviderProps = Partial<StyleContextProps> | Ref<Partial<StyleContextProps>>;
91+
92+
// fix: https://github.com/vueComponent/ant-design-vue/issues/7023
93+
const getCache = () => {
94+
const instance = getCurrentInstance();
95+
let cache: CacheEntity;
96+
if (instance && instance.appContext) {
97+
const globalCache = instance.appContext?.config?.globalProperties?.__ANTDV_CSSINJS_CACHE__;
98+
if (globalCache) {
99+
cache = globalCache;
100+
} else {
101+
cache = createCache();
102+
if (instance.appContext.config.globalProperties) {
103+
instance.appContext.config.globalProperties.__ANTDV_CSSINJS_CACHE__ = cache;
104+
}
105+
}
106+
} else {
107+
cache = createCache();
108+
}
109+
return cache;
110+
};
111+
83112
const defaultStyleContext: StyleContextProps = {
84113
cache: createCache(),
85114
defaultCache: true,
86115
hashPriority: 'low',
87116
};
88117
// fix: https://github.com/vueComponent/ant-design-vue/issues/6912
89118
export const useStyleInject = () => {
90-
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache: createCache() }));
119+
const cache = getCache();
120+
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache }));
91121
};
92122
export const useStyleProvider = (props: UseStyleProviderProps) => {
93123
const parentContext = useStyleInject();

0 commit comments

Comments
 (0)