Skip to content

Commit

Permalink
Scope the cookie to the domain
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Nov 11, 2022
1 parent 0bc27ab commit eb8ca82
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 13 deletions.
12 changes: 1 addition & 11 deletions src/lib/darkMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,13 @@ export function startObservingColorSchemeHtmlAttribute() {

{
const setColorSchemeCookie = (colorScheme: ColorScheme) => {
console.log("DEBUG: setColorSchemeCookie");

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

//We do not set the domain if we are on localhost or an ip
if (window.location.hostname.match(/\.[a-zA-Z]{2,}$/)) {
console.log("DEBUG: Append to newCookie");

newCookie += `;domain=${
window.location.hostname.split(".").length >= 3
? window.location.hostname.replace(/^[^.]+\./, "")
: window.location.hostname
}`;
newCookie += `;domain=${window.location.hostname}`;
}

console.log(`DEBUG: set newCookie: ${newCookie}`);

document.cookie = newCookie;
};

Expand Down
2 changes: 0 additions & 2 deletions src/lib/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ export async function startDsfrReact(params: Params) {
);
}

console.log("DEBUG: startObservingColorSchemeHtmlAttribute");

startObservingColorSchemeHtmlAttribute();

(window as any).dsfr = { verbose, "mode": "manual" };
Expand Down

0 comments on commit eb8ca82

Please sign in to comment.