Skip to content

Commit 8bbafa3

Browse files
committed
Use tldts to get the domain
1 parent 9162956 commit 8bbafa3

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

apps/web-roo-code/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"recharts": "^2.15.3",
3535
"tailwind-merge": "^3.3.0",
3636
"tailwindcss-animate": "^1.0.7",
37+
"tldts": "^6.1.86",
3738
"zod": "^3.25.61"
3839
},
3940
"devDependencies": {

apps/web-roo-code/src/components/CookieConsent.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { useState, useEffect } from "react"
44
import ReactCookieConsent from "react-cookie-consent"
55
import { Cookie } from "lucide-react"
6-
import { CONSENT_COOKIE_NAME, CONSENT_COOKIE_DOMAIN } from "@/lib/constants"
6+
import { CONSENT_COOKIE_NAME, getConsentCookieDomain } from "@/lib/constants"
77

88
export interface CookieConsentProps {
99
/**
@@ -47,14 +47,21 @@ export function CookieConsent({
4747
cookieName = CONSENT_COOKIE_NAME,
4848
debug = false,
4949
}: CookieConsentProps) {
50-
const [hostname, setHostname] = useState<string>(CONSENT_COOKIE_DOMAIN)
50+
const [cookieDomain, setCookieDomain] = useState<string | null>(null)
5151

5252
useEffect(() => {
53-
setHostname(window.location.hostname)
53+
// Get the appropriate domain using tldts
54+
const domain = getConsentCookieDomain()
55+
setCookieDomain(domain)
5456
}, [])
5557

58+
// Don't render until we have a valid domain
59+
if (!cookieDomain) {
60+
return null
61+
}
62+
5663
const extraCookieOptions = {
57-
domain: process.env.NODE_ENV === "production" ? hostname : CONSENT_COOKIE_DOMAIN,
64+
domain: cookieDomain,
5865
}
5966

6067
const containerClasses = `

apps/web-roo-code/src/lib/constants.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ export const INTERNAL_LINKS = {
3131
PRIVACY_POLICY_WEBSITE: "/privacy",
3232
}
3333

34+
import { getDomain } from "tldts"
35+
3436
// Cookie consent settings
3537
export const CONSENT_COOKIE_NAME = "roo-code-cookie-consent"
36-
export const CONSENT_COOKIE_DOMAIN = process.env.CONSENT_COOKIE_DOMAIN || "roocode.com"
38+
39+
/**
40+
* Get the appropriate domain for cookie consent
41+
* Uses tldts to extract the domain from the current hostname
42+
* Falls back to environment variable or roocode.com
43+
* Never returns null/undefined - always returns a valid domain string
44+
*/
45+
export function getConsentCookieDomain(): string {
46+
if (typeof window === "undefined") {
47+
// Server-side fallback
48+
return process.env.CONSENT_COOKIE_DOMAIN || "roocode.com"
49+
}
50+
51+
// Extract the domain using tldts - this can return null for invalid hostnames
52+
const domain = getDomain(window.location.hostname)
53+
54+
// Always return a valid string, never null/undefined
55+
return domain || process.env.CONSENT_COOKIE_DOMAIN || "roocode.com"
56+
}

pnpm-lock.yaml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)