Skip to content

Commit 6f3bba3

Browse files
committed
No script injection when color sheme is set to light #303 (comment)
1 parent 1a065b5 commit 6f3bba3

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

src/next-appdir/DsfrHead.tsx

+24-20
Original file line numberDiff line numberDiff line change
@@ -94,26 +94,30 @@ export function DsfrHead(props: DsfrHeadProps) {
9494
<link rel="shortcut icon" href={getAssetUrl(FaviconIco)} type="image/x-icon" />
9595
</>
9696
)}
97-
<script
98-
suppressHydrationWarning
99-
nonce={nonce}
100-
dangerouslySetInnerHTML={{
101-
"__html": getScriptToRunAsap({
102-
defaultColorScheme,
103-
nonce,
104-
trustedTypesPolicyName
105-
})
106-
}}
107-
/>
108-
{nonce !== undefined && (
109-
<script
110-
suppressHydrationWarning
111-
key="nonce-setter"
112-
nonce={nonce}
113-
dangerouslySetInnerHTML={{
114-
__html: `window.ssrNonce = "${nonce}";`
115-
}}
116-
/>
97+
{defaultColorScheme !== "light" && (
98+
<>
99+
<script
100+
suppressHydrationWarning
101+
nonce={nonce}
102+
dangerouslySetInnerHTML={{
103+
"__html": getScriptToRunAsap({
104+
defaultColorScheme,
105+
nonce,
106+
trustedTypesPolicyName
107+
})
108+
}}
109+
/>
110+
{nonce !== undefined && (
111+
<script
112+
suppressHydrationWarning
113+
key="nonce-setter"
114+
nonce={nonce}
115+
dangerouslySetInnerHTML={{
116+
__html: `window.ssrNonce = "${nonce}";`
117+
}}
118+
/>
119+
)}
120+
</>
117121
)}
118122
</>
119123
);

src/next-pagesdir.tsx

+28-22
Original file line numberDiff line numberDiff line change
@@ -199,31 +199,37 @@ export function createNextDsfrIntegrationApi(
199199
/>
200200
</>
201201
)}
202-
{!isBrowser && ( //NOTE: On browser we handle this manually
202+
{defaultColorScheme !== "light" && (
203203
<>
204-
<style id={rootColorSchemeStyleTagId}>{`:root { color-scheme: ${
205-
isDark ? "dark" : "light"
206-
}; }`}</style>
207-
<meta
208-
name="theme-color"
209-
content={
210-
fr.colors.getHex({ isDark }).decisions.background.default
211-
.grey.default
212-
}
213-
/>
204+
{!isBrowser && ( //NOTE: On browser we handle this manually
205+
<>
206+
<style
207+
id={rootColorSchemeStyleTagId}
208+
>{`:root { color-scheme: ${
209+
isDark ? "dark" : "light"
210+
}; }`}</style>
211+
<meta
212+
name="theme-color"
213+
content={
214+
fr.colors.getHex({ isDark }).decisions.background
215+
.default.grey.default
216+
}
217+
/>
218+
</>
219+
)}
220+
{isProduction && !isBrowser && (
221+
<script
222+
dangerouslySetInnerHTML={{
223+
"__html": getScriptToRunAsap({
224+
defaultColorScheme,
225+
trustedTypesPolicyName,
226+
"nonce": undefined
227+
})
228+
}}
229+
/>
230+
)}
214231
</>
215232
)}
216-
{isProduction && !isBrowser && (
217-
<script
218-
dangerouslySetInnerHTML={{
219-
"__html": getScriptToRunAsap({
220-
defaultColorScheme,
221-
trustedTypesPolicyName,
222-
"nonce": undefined
223-
})
224-
}}
225-
/>
226-
)}
227233
</Head>
228234
{isBrowser ? (
229235
<App {...(props as any)} />

0 commit comments

Comments
 (0)