Skip to content

Commit 58fed4c

Browse files
committed
chore: add in code doc
1 parent 931d99e commit 58fed4c

File tree

6 files changed

+94
-11
lines changed

6 files changed

+94
-11
lines changed

src/next-appdir/DsfrHead.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { setLink, type RegisteredLinkProps } from "../link";
1212
//See: https://github.com/vercel/next.js/issues/16630
1313
// @import url(...) doesn't work. Using Sass and @use is our last resort.
1414
import "../assets/dsfr_plus_icons.scss";
15+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in doc
16+
import { type startReactDsfr } from "./zz_internal/start";
1517

1618
export type DsfrHeadProps = {
1719
/** If not provided no fonts are preloaded.
@@ -20,7 +22,22 @@ export type DsfrHeadProps = {
2022
preloadFonts?: (keyof typeof fontUrlByFileBasename)[];
2123
/** Default: <a /> */
2224
Link?: (props: RegisteredLinkProps & { children: ReactNode }) => ReturnType<React.FC>;
25+
/**
26+
* When set, the value will be used as the nonce attribute of subsequent script tags.
27+
*
28+
* Don't forget to add `checkNonce: true` in {@link startReactDsfr} options.
29+
*
30+
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
31+
*/
2332
nonce?: string;
33+
/**
34+
* Enable Trusted Types with a custom policy name.
35+
*
36+
* Don't forget to add `trustedTypesPolicyName` in {@link startReactDsfr} options.
37+
*
38+
* @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
39+
* @default "react-dsfr"
40+
*/
2441
trustedTypesPolicyName?: string;
2542
};
2643

src/next-appdir/zz_internal/start.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,42 @@ import type { RegisteredLinkProps } from "../../link";
44
import { setLink } from "../../link";
55
import { type DefaultColorScheme, setDefaultColorSchemeClientSide } from "./defaultColorScheme";
66
import { isBrowser } from "../../tools/isBrowser";
7+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in doc
8+
import { type DsfrHead } from "../DsfrHead";
79

810
let isAfterFirstEffect = false;
911
const actions: (() => void)[] = [];
1012

11-
export async function startReactDsfr(params: {
13+
export function startReactDsfr(params: {
1214
defaultColorScheme: DefaultColorScheme;
1315
/** Default: false */
1416
verbose?: boolean;
1517
/** Default: <a /> */
1618
Link?: (props: RegisteredLinkProps & { children: ReactNode }) => ReturnType<React.FC>;
19+
/**
20+
* When true, the nonce of the script tag will be checked, fetched from {@link DsfrHead} component and injected in react-dsfr scripts.
21+
*
22+
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
23+
*/
1724
checkNonce?: boolean;
25+
/**
26+
* Enable Trusted Types with a custom policy name.
27+
*
28+
* Don't forget to also add the policy name in {@link DsfrHead} component.
29+
*
30+
* `<trustedTypesPolicyName>` and `<trustedTypesPolicyName>-asap` should be set in your Content-Security-Policy header.
31+
*
32+
* For example:
33+
* ```txt
34+
* With a policy name of "react-dsfr":
35+
* Content-Security-Policy:
36+
* require-trusted-types-for 'script';
37+
* trusted-types react-dsfr react-dsfr-asap nextjs nextjs#bundler;
38+
* ```
39+
*
40+
* @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
41+
* @default "react-dsfr"
42+
*/
1843
trustedTypesPolicyName?: string;
1944
}) {
2045
const {

src/next-pagesdir.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@ export type CreateNextDsfrIntegrationApiParams = {
4141
doPersistDarkModePreferenceWithCookie?: boolean;
4242
/** Default: ()=> "fr" */
4343
useLang?: () => string;
44-
nonce?: string;
44+
/**
45+
* Enable Trusted Types with a custom policy name.
46+
*
47+
* `<trustedTypesPolicyName>` and `<trustedTypesPolicyName>-asap` should be set in your Content-Security-Policy header.
48+
*
49+
* For example:
50+
* ```txt
51+
* With a policy name of "react-dsfr":
52+
* Content-Security-Policy:
53+
* require-trusted-types-for 'script';
54+
* trusted-types react-dsfr react-dsfr-asap nextjs nextjs#bundler;
55+
* ```
56+
*
57+
* @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
58+
* @default "react-dsfr"
59+
*/
4560
trustedTypesPolicyName?: string;
4661
};
4762

@@ -91,7 +106,6 @@ export function createNextDsfrIntegrationApi(
91106
preloadFonts = [],
92107
doPersistDarkModePreferenceWithCookie = false,
93108
useLang,
94-
nonce,
95109
trustedTypesPolicyName
96110
} = params;
97111

@@ -169,10 +183,9 @@ export function createNextDsfrIntegrationApi(
169183
/>
170184
{!isBrowser && ( //NOTE: On browser we handle this manually
171185
<>
172-
<style
173-
nonce={nonce}
174-
id={rootColorSchemeStyleTagId}
175-
>{`:root { color-scheme: ${isDark ? "dark" : "light"}; }`}</style>
186+
<style id={rootColorSchemeStyleTagId}>{`:root { color-scheme: ${
187+
isDark ? "dark" : "light"
188+
}; }`}</style>
176189
<meta
177190
name="theme-color"
178191
content={
@@ -184,11 +197,9 @@ export function createNextDsfrIntegrationApi(
184197
)}
185198
{isProduction && !isBrowser && (
186199
<script
187-
nonce={nonce}
188200
dangerouslySetInnerHTML={{
189201
"__html": getScriptToRunAsap({
190202
defaultColorScheme,
191-
nonce,
192203
trustedTypesPolicyName
193204
})
194205
}}

src/spa.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,36 @@ export function startReactDsfr(params: {
1515
Link?: (props: RegisteredLinkProps & { children: ReactNode }) => ReturnType<React.FC>;
1616
/** Default: ()=> "fr" */
1717
useLang?: () => string;
18-
checkNonce?: boolean;
18+
/**
19+
* When set, the value will be used as the nonce attribute of subsequent script tags.
20+
*
21+
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
22+
*/
23+
nonce?: string;
24+
/**
25+
* Enable Trusted Types with a custom policy name.
26+
*
27+
* `<trustedTypesPolicyName>` and `<trustedTypesPolicyName>-asap` should be set in your Content-Security-Policy header.
28+
*
29+
* For example:
30+
* ```txt
31+
* With a policy name of "react-dsfr":
32+
* Content-Security-Policy:
33+
* require-trusted-types-for 'script';
34+
* trusted-types react-dsfr react-dsfr-asap nextjs nextjs#bundler;
35+
* ```
36+
*
37+
* @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
38+
* @default "react-dsfr"
39+
*/
1940
trustedTypesPolicyName?: string;
2041
}) {
2142
const {
2243
defaultColorScheme,
2344
verbose = false,
2445
Link,
2546
useLang,
26-
checkNonce,
47+
nonce,
2748
trustedTypesPolicyName
2849
} = params;
2950

@@ -35,6 +56,11 @@ export function startReactDsfr(params: {
3556
setUseLang({ useLang });
3657
}
3758

59+
const checkNonce = !!nonce;
60+
if (checkNonce) {
61+
window.ssrNonce = nonce;
62+
}
63+
3864
start({
3965
defaultColorScheme,
4066
verbose,

test/integration/cra/public/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<meta charset="utf-8" />
66
<meta name="viewport" content="width=device-width, initial-scale=1" />
77
<meta name="description" content="Web site created using create-react-app" />
8+
<meta http-equiv="Content-Security-Policy"
9+
content="require-trusted-types-for 'script'; trusted-types react-dsfr react-dsfr-asap" />
810

911
<link rel="apple-touch-icon" href="%PUBLIC_URL%/dsfr/favicon/apple-touch-icon.png" />
1012
<link rel="icon" href="%PUBLIC_URL%/dsfr/favicon/favicon.svg" type="image/svg+xml" />

test/integration/vite/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<head>
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="Content-Security-Policy"
7+
content="require-trusted-types-for 'script'; trusted-types react-dsfr react-dsfr-asap" />
68

79
<link rel="apple-touch-icon" href="/dsfr/favicon/apple-touch-icon.png" />
810
<link rel="icon" href="/dsfr/favicon/favicon.svg" type="image/svg+xml" />

0 commit comments

Comments
 (0)