Skip to content

Commit 7b0cb34

Browse files
committed
Fix propagation of defaults and undefined not evalued as empty string
1 parent ac66b08 commit 7b0cb34

File tree

10 files changed

+43
-49
lines changed

10 files changed

+43
-49
lines changed

.nvmrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/next-appdir/DsfrHead.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { getScriptToRunAsap } from "../useIsDark/scriptToRunAsap";
88
import { fontUrlByFileBasename } from "./zz_internal/fontUrlByFileBasename";
99
import { getDefaultColorSchemeServerSide } from "./zz_internal/defaultColorScheme";
1010
import { setLink, type RegisteredLinkProps } from "../link";
11+
import { assert } from "tsafe/assert";
1112
//NOTE: As of now there is no way to enforce ordering in Next Appdir
1213
//See: https://github.com/vercel/next.js/issues/16630
1314
// @import url(...) doesn't work. Using Sass and @use is our last resort.
1415
import "../assets/dsfr_plus_icons.scss";
1516
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in doc
1617
import { type startReactDsfr } from "./zz_internal/start";
17-
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../tools/trustedTypesPolicy/config";
1818

1919
export type DsfrHeadProps = {
2020
/** If not provided no fonts are preloaded.
@@ -46,12 +46,9 @@ export type DsfrHeadProps = {
4646
const isProduction = process.env.NODE_ENV !== "development";
4747

4848
export function DsfrHead(props: DsfrHeadProps) {
49-
const {
50-
preloadFonts = [],
51-
Link,
52-
nonce,
53-
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
54-
} = props;
49+
const { preloadFonts = [], Link, nonce, trustedTypesPolicyName = "react-dsfr" } = props;
50+
51+
assert(nonce !== "", "nonce cannot be an empty string");
5552

5653
const defaultColorScheme = getDefaultColorSchemeServerSide();
5754

@@ -90,14 +87,16 @@ export function DsfrHead(props: DsfrHeadProps) {
9087
})
9188
}}
9289
/>
93-
<script
94-
suppressHydrationWarning
95-
key="nonce-setter"
96-
nonce={nonce}
97-
dangerouslySetInnerHTML={{
98-
__html: `window.ssrNonce = "${nonce}";`
99-
}}
100-
/>
90+
{nonce !== undefined && (
91+
<script
92+
suppressHydrationWarning
93+
key="nonce-setter"
94+
nonce={nonce}
95+
dangerouslySetInnerHTML={{
96+
__html: `window.ssrNonce = "${nonce}";`
97+
}}
98+
/>
99+
)}
101100
</>
102101
);
103102
}

src/next-appdir/zz_internal/start.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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";
109

1110
let isAfterFirstEffect = false;
1211
const actions: (() => void)[] = [];
@@ -21,6 +20,7 @@ export function startReactDsfr(params: {
2120
* When true, the nonce of the script tag will be checked, fetched from {@link DsfrHead} component and injected in react-dsfr scripts.
2221
*
2322
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
23+
* @default false
2424
*/
2525
doCheckNonce?: boolean;
2626
/**
@@ -49,7 +49,7 @@ export function startReactDsfr(params: {
4949
verbose = false,
5050
Link,
5151
doCheckNonce = false,
52-
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
52+
trustedTypesPolicyName = "react-dsfr"
5353
} = params;
5454

5555
setDefaultColorSchemeClientSide({ defaultColorScheme });
@@ -62,6 +62,8 @@ export function startReactDsfr(params: {
6262
start({
6363
defaultColorScheme,
6464
verbose,
65+
doCheckNonce,
66+
trustedTypesPolicyName,
6567
"nextParams": {
6668
"doPersistDarkModePreferenceWithCookie": false,
6769
"registerEffectAction": action => {
@@ -71,9 +73,7 @@ export function startReactDsfr(params: {
7173
actions.push(action);
7274
}
7375
}
74-
},
75-
doCheckNonce,
76-
trustedTypesPolicyName
76+
}
7777
});
7878
}
7979
}

src/next-pagesdir.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function createNextDsfrIntegrationApi(
106106
preloadFonts = [],
107107
doPersistDarkModePreferenceWithCookie = false,
108108
useLang,
109-
trustedTypesPolicyName
109+
trustedTypesPolicyName = "react-dsfr"
110110
} = params;
111111

112112
let isAfterFirstEffect = false;
@@ -124,6 +124,8 @@ export function createNextDsfrIntegrationApi(
124124
start({
125125
defaultColorScheme,
126126
verbose,
127+
"doCheckNonce": false,
128+
trustedTypesPolicyName,
127129
"nextParams": {
128130
doPersistDarkModePreferenceWithCookie,
129131
"registerEffectAction": action => {
@@ -200,7 +202,8 @@ export function createNextDsfrIntegrationApi(
200202
dangerouslySetInnerHTML={{
201203
"__html": getScriptToRunAsap({
202204
defaultColorScheme,
203-
trustedTypesPolicyName
205+
trustedTypesPolicyName,
206+
"nonce": undefined
204207
})
205208
}}
206209
/>

src/spa.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +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";
7+
import { assert } from "tsafe/assert";
88

99
export type { RegisterLink, RegisteredLinkProps };
1010

@@ -47,7 +47,7 @@ export function startReactDsfr(params: {
4747
Link,
4848
useLang,
4949
nonce,
50-
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
50+
trustedTypesPolicyName = "react-dsfr"
5151
} = params;
5252

5353
if (Link !== undefined) {
@@ -58,17 +58,19 @@ export function startReactDsfr(params: {
5858
setUseLang({ useLang });
5959
}
6060

61-
const doCheckNonce = !!nonce; // handle undefined and empty string
61+
assert(nonce !== "", "nonce cannot be an empty string");
62+
63+
const doCheckNonce = nonce !== undefined;
6264
if (doCheckNonce) {
6365
window.ssrNonce = nonce;
6466
}
6567

6668
start({
6769
defaultColorScheme,
6870
verbose,
69-
"nextParams": undefined,
7071
doCheckNonce,
71-
trustedTypesPolicyName
72+
trustedTypesPolicyName,
73+
"nextParams": undefined
7274
});
7375
}
7476

src/start.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ 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";
76

87
type Params = {
98
defaultColorScheme: ColorScheme | "system";
@@ -14,20 +13,15 @@ type Params = {
1413
registerEffectAction: (effect: () => void) => void;
1514
}
1615
| undefined;
17-
doCheckNonce?: boolean;
18-
trustedTypesPolicyName?: string;
16+
doCheckNonce: boolean;
17+
trustedTypesPolicyName: string;
1918
};
2019

2120
let isStarted = false;
2221

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

3226
assert(isBrowser);
3327

src/tools/trustedTypesPolicy/config.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/useIsDark/client.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ 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";
76

87
export type ColorScheme = "light" | "dark";
98

@@ -99,15 +98,15 @@ export function startClientSideIsDarkLogic(params: {
9998
registerEffectAction: (action: () => void) => void;
10099
doPersistDarkModePreferenceWithCookie: boolean;
101100
colorSchemeExplicitlyProvidedAsParameter: ColorScheme | "system";
102-
doCheckNonce?: boolean;
103-
trustedTypesPolicyName?: string;
101+
doCheckNonce: boolean;
102+
trustedTypesPolicyName: string;
104103
}) {
105104
const {
106105
doPersistDarkModePreferenceWithCookie,
107106
registerEffectAction,
108107
colorSchemeExplicitlyProvidedAsParameter,
109108
doCheckNonce = false,
110-
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
109+
trustedTypesPolicyName
111110
} = params;
112111

113112
const { clientSideIsDark, ssrWasPerformedWithIsDark: ssrWasPerformedWithIsDark_ } = ((): {

src/useIsDark/scriptToRunAsap.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
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";
54

65
type GetScriptToRunAsap = (props: {
76
defaultColorScheme: ColorScheme | "system";
8-
nonce?: string;
9-
trustedTypesPolicyName?: string;
7+
nonce: string | undefined;
8+
trustedTypesPolicyName: string;
109
}) => string;
1110

1211
declare global {
@@ -19,8 +18,8 @@ declare global {
1918
// TODO enhance to use DOMPurify with trustedTypes
2019
export const getScriptToRunAsap: GetScriptToRunAsap = ({
2120
defaultColorScheme,
22-
nonce,
23-
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
21+
nonce = "",
22+
trustedTypesPolicyName
2423
}) => `
2524
{
2625

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-
doCheckNonce: true
17+
"doCheckNonce": true
1818
});
1919

2020
export function StartDsfr(){

0 commit comments

Comments
 (0)