Skip to content

Commit 5360a5b

Browse files
committed
refactor: extract nonce handling logic into shared function
提取了 useCacheToken 和 useCSSVarRegister 中重复的 nonce 处理逻辑为 mergeCSSConfig 共享函数,减少代码重复并提高可维护性。 Changes: - 在 util/index.ts 中添加 mergeCSSConfig 工具函数 - useCacheToken 使用 mergeCSSConfig 代替重复代码 - useCSSVarRegister 使用 mergeCSSConfig 代替重复代码
1 parent 0082730 commit 5360a5b

3 files changed

Lines changed: 39 additions & 24 deletions

File tree

src/hooks/useCSSVarRegister.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import StyleContext, {
55
ATTR_TOKEN,
66
CSS_IN_JS_INSTANCE,
77
} from '../StyleContext';
8-
import { isClientSide, toStyleStr } from '../util';
8+
import { isClientSide, mergeCSSConfig, toStyleStr } from '../util';
99
import type { TokenWithCSSVar } from '../util/css-variables';
1010
import { transformToken } from '../util/css-variables';
1111
import type { ExtractStyle } from './useGlobalCache';
@@ -71,17 +71,15 @@ const useCSSVarRegister = <V, T extends Record<string, V>>(
7171
if (!cssVarsStr) {
7272
return;
7373
}
74-
const mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
75-
mark: ATTR_MARK,
76-
prepend: 'queue',
77-
attachTo: container,
78-
priority: -999,
79-
};
80-
81-
const nonceStr = typeof nonce === 'function' ? nonce() : nonce;
82-
if (nonceStr) {
83-
mergedCSSConfig.csp = { nonce: nonceStr };
84-
}
74+
const mergedCSSConfig = mergeCSSConfig<Parameters<typeof updateCSS>[2]>(
75+
{
76+
mark: ATTR_MARK,
77+
prepend: 'queue',
78+
attachTo: container,
79+
priority: -999,
80+
},
81+
nonce,
82+
);
8583

8684
const style = updateCSS(cssVarsStr, styleId, mergedCSSConfig);
8785

src/hooks/useCacheToken.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import StyleContext, {
77
CSS_IN_JS_INSTANCE,
88
} from '../StyleContext';
99
import type Theme from '../theme/Theme';
10-
import { flattenToken, memoResult, token2key, toStyleStr } from '../util';
10+
import { flattenToken, memoResult, mergeCSSConfig, token2key, toStyleStr } from '../util';
1111
import { transformToken } from '../util/css-variables';
1212
import type { ExtractStyle } from './useGlobalCache';
1313
import useGlobalCache from './useGlobalCache';
@@ -220,17 +220,15 @@ export default function useCacheToken<
220220
if (!cssVarsStr) {
221221
return;
222222
}
223-
const mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
224-
mark: ATTR_MARK,
225-
prepend: 'queue',
226-
attachTo: container,
227-
priority: -999,
228-
};
229-
230-
const nonceStr = typeof nonce === 'function' ? nonce() : nonce;
231-
if (nonceStr) {
232-
mergedCSSConfig.csp = { nonce: nonceStr };
233-
}
223+
const mergedCSSConfig = mergeCSSConfig<Parameters<typeof updateCSS>[2]>(
224+
{
225+
mark: ATTR_MARK,
226+
prepend: 'queue',
227+
attachTo: container,
228+
priority: -999,
229+
},
230+
nonce,
231+
);
234232

235233
const style = updateCSS(cssVarsStr, hash(`css-var-${themeKey}`), mergedCSSConfig);
236234

src/util/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ export function supportLogicProps(): boolean {
155155

156156
export const isClientSide = canUseDom();
157157

158+
/**
159+
* Merge CSS injection configuration with nonce support
160+
*/
161+
export function mergeCSSConfig<T extends Record<string, any>>(
162+
config: T,
163+
nonce?: string | (() => string),
164+
): T {
165+
if (!nonce) {
166+
return config;
167+
}
168+
169+
const mergedConfig = { ...config };
170+
const nonceStr = typeof nonce === 'function' ? nonce() : nonce;
171+
if (nonceStr) {
172+
(mergedConfig as any).csp = { nonce: nonceStr };
173+
}
174+
175+
return mergedConfig;
176+
}
158177
export function unit(num: string | number) {
159178
if (typeof num === 'number') {
160179
return `${num}px`;

0 commit comments

Comments
 (0)