Skip to content

Commit ede79c0

Browse files
committed
Refactor injectCSPNonce to return new config object
- Change injectCSPNonce from mutating to returning new object - Follow functional programming principles - Preserve existing csp config with spread operator - Update all call sites to use returned value
1 parent a43d123 commit ede79c0

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

src/hooks/useCSSVarRegister.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ const useCSSVarRegister = <V, T extends Record<string, V>>(
7171
if (!cssVarsStr) {
7272
return;
7373
}
74-
const mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
74+
let mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
7575
mark: ATTR_MARK,
7676
prepend: 'queue',
7777
attachTo: container,
7878
priority: -999,
7979
};
8080

81-
injectCSPNonce(mergedCSSConfig, nonce);
81+
mergedCSSConfig = injectCSPNonce(mergedCSSConfig, nonce);
8282

8383
const style = updateCSS(cssVarsStr, styleId, mergedCSSConfig);
8484

src/hooks/useCacheToken.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import StyleContext, {
77
CSS_IN_JS_INSTANCE,
88
} from '../StyleContext';
99
import type Theme from '../theme/Theme';
10-
import { flattenToken, injectCSPNonce, memoResult, token2key, toStyleStr } from '../util';
10+
import {
11+
flattenToken,
12+
injectCSPNonce,
13+
memoResult,
14+
token2key,
15+
toStyleStr,
16+
} from '../util';
1117
import { transformToken } from '../util/css-variables';
1218
import type { ExtractStyle } from './useGlobalCache';
1319
import useGlobalCache from './useGlobalCache';
@@ -226,16 +232,20 @@ export default function useCacheToken<
226232
if (!cssVarsStr) {
227233
return;
228234
}
229-
const mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
235+
let mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
230236
mark: ATTR_MARK,
231237
prepend: 'queue',
232238
attachTo: container,
233239
priority: -999,
234240
};
235241

236-
injectCSPNonce(mergedCSSConfig, nonce);
242+
mergedCSSConfig = injectCSPNonce(mergedCSSConfig, nonce);
237243

238-
const style = updateCSS(cssVarsStr, hash(`css-var-${themeKey}`), mergedCSSConfig);
244+
const style = updateCSS(
245+
cssVarsStr,
246+
hash(`css-var-${themeKey}`),
247+
mergedCSSConfig,
248+
);
239249

240250
(style as any)[CSS_IN_JS_INSTANCE] = instanceId;
241251

src/hooks/useStyleRegister.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ import StyleContext, {
1515
ATTR_MARK,
1616
CSS_IN_JS_INSTANCE,
1717
} from '../StyleContext';
18-
import { injectCSPNonce, isClientSide, isNonNullable, toStyleStr, where } from '../util';
18+
import {
19+
injectCSPNonce,
20+
isClientSide,
21+
isNonNullable,
22+
toStyleStr,
23+
where,
24+
} from '../util';
1925
import {
2026
CSS_FILE_STYLE,
2127
existPath,
@@ -456,14 +462,14 @@ export default function useStyleRegister(
456462
(cacheValue) => {
457463
const [styleStr, styleId, effectStyle, , priority] = cacheValue;
458464
if (isMergedClientSide && styleStr !== CSS_FILE_STYLE) {
459-
const mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
465+
let mergedCSSConfig: Parameters<typeof updateCSS>[2] = {
460466
mark: ATTR_MARK,
461467
prepend: enableLayer ? false : 'queue',
462468
attachTo: container,
463469
priority,
464470
};
465471

466-
injectCSPNonce(mergedCSSConfig, nonce);
472+
mergedCSSConfig = injectCSPNonce(mergedCSSConfig, nonce);
467473

468474
// ================= Split Effect Style =================
469475
// We will split effectStyle here since @layer should be at the top level

src/util/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,16 @@ export type Nonce = string | (() => string);
213213
export function injectCSPNonce<T extends { csp?: { nonce?: string } }>(
214214
config: T,
215215
nonce: Nonce | undefined,
216-
) {
216+
): T {
217217
const nonceStr = typeof nonce === 'function' ? nonce() : nonce;
218218
if (nonceStr) {
219-
config.csp = { ...config.csp, nonce: nonceStr };
219+
return {
220+
...config,
221+
csp: {
222+
...config.csp,
223+
nonce: nonceStr,
224+
},
225+
};
220226
}
227+
return config;
221228
}

0 commit comments

Comments
 (0)