Skip to content

Commit 8745964

Browse files
committed
Do not set cookies when we don't use a document
1 parent a8a1f19 commit 8745964

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/lib/darkMode.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ function getCurrentIsDarkFromHtmlAttribute(): boolean {
7272
assert(false);
7373
}
7474

75+
export const refDoPersistDarkModePreferenceWithCookie = { "current": false };
76+
7577
export function startObservingColorSchemeHtmlAttribute() {
7678
$isDark.current = getCurrentIsDarkFromHtmlAttribute();
7779

@@ -84,26 +86,11 @@ export function startObservingColorSchemeHtmlAttribute() {
8486
);
8587

8688
{
87-
/*
88-
const setColorSchemeCookie = (colorScheme: ColorScheme) => {
89-
let newCookie = `${data_fr_theme}=${colorScheme};path=/;max-age=31536000`;
90-
91-
set_domain: {
92-
const { hostname } = window.location;
93-
94-
//We do not set the domain if we are on localhost or an ip
95-
if (/(^localhost$)|(^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$)/.test(hostname)) {
96-
break set_domain;
97-
}
98-
99-
newCookie += `;domain=${hostname}`;
89+
const setColorSchemeCookie = (isDark: boolean) => {
90+
if (!refDoPersistDarkModePreferenceWithCookie.current) {
91+
return;
10092
}
10193

102-
document.cookie = newCookie;
103-
};
104-
*/
105-
106-
const setColorSchemeCookie = (isDark: boolean) => {
10794
const colorScheme: ColorScheme = isDark ? "dark" : "light";
10895

10996
let newCookie = `${data_fr_theme}=${colorScheme};path=/;max-age=31536000;SameSite=Strict`;

src/next.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import appleTouchIcon from "./dsfr/favicon/apple-touch-icon.png";
2222
import faviconSvg from "./dsfr/favicon/favicon.svg";
2323
import faviconIco from "./dsfr/favicon/favicon.ico";
2424
import faviconWebmanifestUrl from "./dsfr/favicon/manifest.webmanifest";
25-
import { data_fr_scheme, data_fr_theme, isDarkContext } from "./lib/darkMode";
25+
import {
26+
data_fr_scheme,
27+
data_fr_theme,
28+
isDarkContext,
29+
refDoPersistDarkModePreferenceWithCookie
30+
} from "./lib/darkMode";
2631
import type { ColorScheme } from "./lib/darkMode";
2732
import DefaultDocument from "next/document";
2833

@@ -119,6 +124,10 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
119124
...startDsfrReactParams
120125
} = params;
121126

127+
if (doPersistDarkModePreferenceWithCookie) {
128+
refDoPersistDarkModePreferenceWithCookie.current = true;
129+
}
130+
122131
if (isBrowser) {
123132
startDsfrReact(startDsfrReactParams);
124133
} else {

0 commit comments

Comments
 (0)