Skip to content

Commit

Permalink
Fix propagation of defaults and undefined not evalued as empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Sep 29, 2023
1 parent ac66b08 commit dc341f5
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 39 deletions.
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

24 changes: 14 additions & 10 deletions src/next-appdir/DsfrHead.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { getScriptToRunAsap } from "../useIsDark/scriptToRunAsap";
import { fontUrlByFileBasename } from "./zz_internal/fontUrlByFileBasename";
import { getDefaultColorSchemeServerSide } from "./zz_internal/defaultColorScheme";
import { setLink, type RegisteredLinkProps } from "../link";
import { assert } from "tsafe/assert";
//NOTE: As of now there is no way to enforce ordering in Next Appdir
//See: https://github.com/vercel/next.js/issues/16630
// @import url(...) doesn't work. Using Sass and @use is our last resort.
import "../assets/dsfr_plus_icons.scss";
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in doc
import { type startReactDsfr } from "./zz_internal/start";
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../tools/trustedTypesPolicy/config";

export type DsfrHeadProps = {
/** If not provided no fonts are preloaded.
Expand Down Expand Up @@ -50,9 +50,11 @@ export function DsfrHead(props: DsfrHeadProps) {
preloadFonts = [],
Link,
nonce,
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
trustedTypesPolicyName = "react-dsfr"
} = props;

assert(nonce !== "", "nonce cannot be an empty string");

const defaultColorScheme = getDefaultColorSchemeServerSide();

useMemo(() => {
Expand Down Expand Up @@ -90,14 +92,16 @@ export function DsfrHead(props: DsfrHeadProps) {
})
}}
/>
<script
suppressHydrationWarning
key="nonce-setter"
nonce={nonce}
dangerouslySetInnerHTML={{
__html: `window.ssrNonce = "${nonce}";`
}}
/>
{nonce !== undefined &&
<script
suppressHydrationWarning
key="nonce-setter"
nonce={nonce}
dangerouslySetInnerHTML={{
__html: `window.ssrNonce = "${nonce}";`
}}
/>
}
</>
);
}
10 changes: 5 additions & 5 deletions src/next-appdir/zz_internal/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { type DefaultColorScheme, setDefaultColorSchemeClientSide } from "./defa
import { isBrowser } from "../../tools/isBrowser";
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- used in doc
import { type DsfrHead } from "../DsfrHead";
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../../tools/trustedTypesPolicy/config";

let isAfterFirstEffect = false;
const actions: (() => void)[] = [];
Expand All @@ -21,6 +20,7 @@ export function startReactDsfr(params: {
* When true, the nonce of the script tag will be checked, fetched from {@link DsfrHead} component and injected in react-dsfr scripts.
*
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
* @default false
*/
doCheckNonce?: boolean;
/**
Expand Down Expand Up @@ -49,7 +49,7 @@ export function startReactDsfr(params: {
verbose = false,
Link,
doCheckNonce = false,
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
trustedTypesPolicyName = "react-dsfr"
} = params;

setDefaultColorSchemeClientSide({ defaultColorScheme });
Expand All @@ -62,6 +62,8 @@ export function startReactDsfr(params: {
start({
defaultColorScheme,
verbose,
doCheckNonce,
trustedTypesPolicyName,
"nextParams": {
"doPersistDarkModePreferenceWithCookie": false,
"registerEffectAction": action => {
Expand All @@ -71,9 +73,7 @@ export function startReactDsfr(params: {
actions.push(action);
}
}
},
doCheckNonce,
trustedTypesPolicyName
}
});
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/next-pagesdir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function createNextDsfrIntegrationApi(
preloadFonts = [],
doPersistDarkModePreferenceWithCookie = false,
useLang,
trustedTypesPolicyName
trustedTypesPolicyName = "react-dsfr"
} = params;

let isAfterFirstEffect = false;
Expand All @@ -124,6 +124,8 @@ export function createNextDsfrIntegrationApi(
start({
defaultColorScheme,
verbose,
"doCheckNonce": false,
trustedTypesPolicyName,
"nextParams": {
doPersistDarkModePreferenceWithCookie,
"registerEffectAction": action => {
Expand Down Expand Up @@ -200,7 +202,8 @@ export function createNextDsfrIntegrationApi(
dangerouslySetInnerHTML={{
"__html": getScriptToRunAsap({
defaultColorScheme,
trustedTypesPolicyName
trustedTypesPolicyName,
"nonce": undefined
})
}}
/>
Expand Down
12 changes: 7 additions & 5 deletions src/spa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { RegisterLink, RegisteredLinkProps } from "./link";
import { setLink } from "./link";
import { setUseLang } from "./i18n";
import type { ColorScheme } from "./useIsDark";
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "./tools/trustedTypesPolicy/config";
import { assert } from "tsafe/assert";

export type { RegisterLink, RegisteredLinkProps };

Expand Down Expand Up @@ -47,7 +47,7 @@ export function startReactDsfr(params: {
Link,
useLang,
nonce,
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
trustedTypesPolicyName = "react-dsfr"
} = params;

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

const doCheckNonce = !!nonce; // handle undefined and empty string
assert(nonce !== "", "nonce cannot be an empty string");

const doCheckNonce = nonce !== undefined;
if (doCheckNonce) {
window.ssrNonce = nonce;
}

start({
defaultColorScheme,
verbose,
"nextParams": undefined,
doCheckNonce,
trustedTypesPolicyName
trustedTypesPolicyName,
"nextParams": undefined
});
}

Expand Down
9 changes: 4 additions & 5 deletions src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { assert } from "tsafe/assert";
import type { ColorScheme } from "./useIsDark";
import { startClientSideIsDarkLogic } from "./useIsDark/client";
import { Deferred } from "./tools/Deferred";
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "./tools/trustedTypesPolicy/config";

type Params = {
defaultColorScheme: ColorScheme | "system";
Expand All @@ -14,8 +13,8 @@ type Params = {
registerEffectAction: (effect: () => void) => void;
}
| undefined;
doCheckNonce?: boolean;
trustedTypesPolicyName?: string;
doCheckNonce: boolean;
trustedTypesPolicyName: string;
};

let isStarted = false;
Expand All @@ -25,8 +24,8 @@ export async function start(params: Params) {
defaultColorScheme,
verbose,
nextParams,
doCheckNonce = false,
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
doCheckNonce,
trustedTypesPolicyName
} = params;

assert(isBrowser);
Expand Down
1 change: 0 additions & 1 deletion src/tools/trustedTypesPolicy/config.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/useIsDark/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createStatefulObservable, useRerenderOnChange } from "../tools/Stateful
import { useConstCallback } from "../tools/powerhooks/useConstCallback";
import { fr } from "../fr";
import { data_fr_scheme, data_fr_theme, rootColorSchemeStyleTagId } from "./constants";
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../tools/trustedTypesPolicy/config";

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

Expand Down Expand Up @@ -99,15 +98,15 @@ export function startClientSideIsDarkLogic(params: {
registerEffectAction: (action: () => void) => void;
doPersistDarkModePreferenceWithCookie: boolean;
colorSchemeExplicitlyProvidedAsParameter: ColorScheme | "system";
doCheckNonce?: boolean;
trustedTypesPolicyName?: string;
doCheckNonce: boolean;
trustedTypesPolicyName: string;
}) {
const {
doPersistDarkModePreferenceWithCookie,
registerEffectAction,
colorSchemeExplicitlyProvidedAsParameter,
doCheckNonce = false,
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
trustedTypesPolicyName
} = params;

const { clientSideIsDark, ssrWasPerformedWithIsDark: ssrWasPerformedWithIsDark_ } = ((): {
Expand Down
9 changes: 4 additions & 5 deletions src/useIsDark/scriptToRunAsap.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { ColorScheme } from "./client";
import { data_fr_scheme, data_fr_theme, rootColorSchemeStyleTagId } from "./constants";
import { fr } from "../fr";
import { DEFAULT_TRUSTED_TYPES_POLICY_NAME } from "../tools/trustedTypesPolicy/config";

type GetScriptToRunAsap = (props: {
defaultColorScheme: ColorScheme | "system";
nonce?: string;
trustedTypesPolicyName?: string;
nonce: string | undefined;
trustedTypesPolicyName: string;
}) => string;

declare global {
Expand All @@ -19,8 +18,8 @@ declare global {
// TODO enhance to use DOMPurify with trustedTypes
export const getScriptToRunAsap: GetScriptToRunAsap = ({
defaultColorScheme,
nonce,
trustedTypesPolicyName = DEFAULT_TRUSTED_TYPES_POLICY_NAME
nonce = "",
trustedTypesPolicyName
}) => `
{
Expand Down
2 changes: 1 addition & 1 deletion test/integration/next-appdir/app/StartDsfr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ declare module "@codegouvfr/react-dsfr/next-appdir" {
startReactDsfr({
defaultColorScheme,
Link,
doCheckNonce: true
"doCheckNonce": true
});

export function StartDsfr(){
Expand Down

0 comments on commit dc341f5

Please sign in to comment.