Skip to content

Commit 12f1e12

Browse files
committed
chore: review
1 parent a0e36a0 commit 12f1e12

File tree

8 files changed

+40
-19
lines changed

8 files changed

+40
-19
lines changed

src/next-appdir/DsfrHead.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { setLink, type RegisteredLinkProps } from "../link";
1414
import "../assets/dsfr_plus_icons.scss";
1515
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in doc
1616
import { type startReactDsfr } from "./zz_internal/start";
17+
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../tools/trustedTypesPolicy/config";
1718

1819
export type DsfrHeadProps = {
1920
/** If not provided no fonts are preloaded.
@@ -25,7 +26,7 @@ export type DsfrHeadProps = {
2526
/**
2627
* When set, the value will be used as the nonce attribute of subsequent script tags.
2728
*
28-
* Don't forget to add `checkNonce: true` in {@link startReactDsfr} options.
29+
* Don't forget to add `doCheckNonce: true` in {@link startReactDsfr} options.
2930
*
3031
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
3132
*/
@@ -36,6 +37,7 @@ export type DsfrHeadProps = {
3637
* Don't forget to add `trustedTypesPolicyName` in {@link startReactDsfr} options.
3738
*
3839
* @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
40+
* @see {@link DEFAULT_TRUSTED_TYPES_POLICY_NAME}
3941
* @default "react-dsfr"
4042
*/
4143
trustedTypesPolicyName?: string;
@@ -44,7 +46,12 @@ export type DsfrHeadProps = {
4446
const isProduction = process.env.NODE_ENV !== "development";
4547

4648
export function DsfrHead(props: DsfrHeadProps) {
47-
const { preloadFonts = [], Link, nonce, trustedTypesPolicyName } = props;
49+
const {
50+
preloadFonts = [],
51+
Link,
52+
nonce,
53+
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
54+
} = props;
4855

4956
const defaultColorScheme = getDefaultColorSchemeServerSide();
5057

src/next-appdir/zz_internal/start.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { type DefaultColorScheme, setDefaultColorSchemeClientSide } from "./defa
66
import { isBrowser } from "../../tools/isBrowser";
77
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in doc
88
import { type DsfrHead } from "../DsfrHead";
9+
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../../tools/trustedTypesPolicy/config";
910

1011
let isAfterFirstEffect = false;
1112
const actions: (() => void)[] = [];
@@ -21,7 +22,7 @@ export function startReactDsfr(params: {
2122
*
2223
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
2324
*/
24-
checkNonce?: boolean;
25+
doCheckNonce?: boolean;
2526
/**
2627
* Enable Trusted Types with a custom policy name.
2728
*
@@ -38,6 +39,7 @@ export function startReactDsfr(params: {
3839
* ```
3940
*
4041
* @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
42+
* @see {@link DEFAULT_TRUSTED_TYPES_POLICY_NAME}
4143
* @default "react-dsfr"
4244
*/
4345
trustedTypesPolicyName?: string;
@@ -46,8 +48,8 @@ export function startReactDsfr(params: {
4648
defaultColorScheme,
4749
verbose = false,
4850
Link,
49-
checkNonce,
50-
trustedTypesPolicyName
51+
doCheckNonce = false,
52+
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
5153
} = params;
5254

5355
setDefaultColorSchemeClientSide({ defaultColorScheme });
@@ -70,7 +72,7 @@ export function startReactDsfr(params: {
7072
}
7173
}
7274
},
73-
checkNonce,
75+
doCheckNonce,
7476
trustedTypesPolicyName
7577
});
7678
}

src/spa.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { RegisterLink, RegisteredLinkProps } from "./link";
44
import { setLink } from "./link";
55
import { setUseLang } from "./i18n";
66
import type { ColorScheme } from "./useIsDark";
7+
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "./tools/trustedTypesPolicy/config";
78

89
export type { RegisterLink, RegisteredLinkProps };
910

@@ -35,6 +36,7 @@ export function startReactDsfr(params: {
3536
* ```
3637
*
3738
* @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
39+
* @see {@link DEFAULT_TRUSTED_TYPES_POLICY_NAME}
3840
* @default "react-dsfr"
3941
*/
4042
trustedTypesPolicyName?: string;
@@ -45,7 +47,7 @@ export function startReactDsfr(params: {
4547
Link,
4648
useLang,
4749
nonce,
48-
trustedTypesPolicyName
50+
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
4951
} = params;
5052

5153
if (Link !== undefined) {
@@ -56,16 +58,16 @@ export function startReactDsfr(params: {
5658
setUseLang({ useLang });
5759
}
5860

59-
const checkNonce = !!nonce;
60-
if (checkNonce) {
61+
const doCheckNonce = !!nonce; // handle undefined and empty string
62+
if (doCheckNonce) {
6163
window.ssrNonce = nonce;
6264
}
6365

6466
start({
6567
defaultColorScheme,
6668
verbose,
6769
"nextParams": undefined,
68-
checkNonce,
70+
doCheckNonce,
6971
trustedTypesPolicyName
7072
});
7173
}

src/start.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert } from "tsafe/assert";
33
import type { ColorScheme } from "./useIsDark";
44
import { startClientSideIsDarkLogic } from "./useIsDark/client";
55
import { Deferred } from "./tools/Deferred";
6+
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "./tools/trustedTypesPolicy/config";
67

78
type Params = {
89
defaultColorScheme: ColorScheme | "system";
@@ -13,14 +14,20 @@ type Params = {
1314
registerEffectAction: (effect: () => void) => void;
1415
}
1516
| undefined;
16-
checkNonce?: boolean;
17+
doCheckNonce?: boolean;
1718
trustedTypesPolicyName?: string;
1819
};
1920

2021
let isStarted = false;
2122

2223
export async function start(params: Params) {
23-
const { defaultColorScheme, verbose, nextParams, checkNonce, trustedTypesPolicyName } = params;
24+
const {
25+
defaultColorScheme,
26+
verbose,
27+
nextParams,
28+
doCheckNonce = false,
29+
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
30+
} = params;
2431

2532
assert(isBrowser);
2633

@@ -38,7 +45,7 @@ export async function start(params: Params) {
3845
"doPersistDarkModePreferenceWithCookie":
3946
nextParams === undefined ? false : nextParams.doPersistDarkModePreferenceWithCookie,
4047
registerEffectAction,
41-
checkNonce,
48+
doCheckNonce,
4249
trustedTypesPolicyName
4350
});
4451

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DEFAULT_TRUSTED_TYPES_POLICY_NAME = "react-dsfr";

src/useIsDark/client.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { createStatefulObservable, useRerenderOnChange } from "../tools/Stateful
33
import { useConstCallback } from "../tools/powerhooks/useConstCallback";
44
import { fr } from "../fr";
55
import { data_fr_scheme, data_fr_theme, rootColorSchemeStyleTagId } from "./constants";
6+
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../tools/trustedTypesPolicy/config";
67

78
export type ColorScheme = "light" | "dark";
89

@@ -98,15 +99,15 @@ export function startClientSideIsDarkLogic(params: {
9899
registerEffectAction: (action: () => void) => void;
99100
doPersistDarkModePreferenceWithCookie: boolean;
100101
colorSchemeExplicitlyProvidedAsParameter: ColorScheme | "system";
101-
checkNonce?: boolean;
102+
doCheckNonce?: boolean;
102103
trustedTypesPolicyName?: string;
103104
}) {
104105
const {
105106
doPersistDarkModePreferenceWithCookie,
106107
registerEffectAction,
107108
colorSchemeExplicitlyProvidedAsParameter,
108-
checkNonce,
109-
trustedTypesPolicyName = "react-dsfr"
109+
doCheckNonce = false,
110+
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
110111
} = params;
111112

112113
const { clientSideIsDark, ssrWasPerformedWithIsDark: ssrWasPerformedWithIsDark_ } = ((): {
@@ -234,7 +235,7 @@ export function startClientSideIsDarkLogic(params: {
234235
{
235236
const setRootColorScheme = (isDark: boolean) => {
236237
const nonce = window.ssrNonce;
237-
if (checkNonce && !nonce) {
238+
if (doCheckNonce && !nonce) {
238239
return;
239240
}
240241
document.getElementById(rootColorSchemeStyleTagId)?.remove();

src/useIsDark/scriptToRunAsap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ColorScheme } from "./client";
22
import { data_fr_scheme, data_fr_theme, rootColorSchemeStyleTagId } from "./constants";
33
import { fr } from "../fr";
4+
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../tools/trustedTypesPolicy/config";
45

56
type GetScriptToRunAsap = (props: {
67
defaultColorScheme: ColorScheme | "system";
@@ -19,7 +20,7 @@ declare global {
1920
export const getScriptToRunAsap: GetScriptToRunAsap = ({
2021
defaultColorScheme,
2122
nonce,
22-
trustedTypesPolicyName = "react-dsfr"
23+
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
2324
}) => `
2425
{
2526

test/integration/next-appdir/app/StartDsfr.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare module "@codegouvfr/react-dsfr/next-appdir" {
1414
startReactDsfr({
1515
defaultColorScheme,
1616
Link,
17-
checkNonce: true
17+
doCheckNonce: true
1818
});
1919

2020
export function StartDsfr(){

0 commit comments

Comments
 (0)