Skip to content

Commit f553adb

Browse files
committed
Better regexp to tell if we are running locally
1 parent 571b150 commit f553adb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/lib/darkMode.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ export function startObservingColorSchemeHtmlAttribute() {
7474
const setColorSchemeCookie = (colorScheme: ColorScheme) => {
7575
let newCookie = `${data_fr_theme}=${colorScheme};path=/;max-age=31536000`;
7676

77-
//We do not set the domain if we are on localhost or an ip
78-
if (window.location.hostname.match(/\.[a-zA-Z]{2,}$/)) {
79-
newCookie += `;domain=${window.location.hostname}`;
77+
set_domain: {
78+
const { hostname } = window.location;
79+
80+
//We do not set the domain if we are on localhost or an ip
81+
if (/(^localhost$)|(^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$)/.test(hostname)) {
82+
break set_domain;
83+
}
84+
85+
newCookie += `;domain=${hostname}`;
8086
}
8187

8288
document.cookie = newCookie;

0 commit comments

Comments
 (0)