Skip to content

Commit 61c88e4

Browse files
committed
fix: change set local cookie
1 parent cdc7379 commit 61c88e4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/ui/src/components/locale-switcher.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ const localeNames: Record<string, string> = {
2828
function setLocaleCookie(locale: string) {
2929
const hostname = window.location.hostname
3030
const parts = hostname.split('.')
31-
const rootDomain = parts.length >= 2 ? '.' + parts.slice(-2).join('.') : hostname
32-
document.cookie = `locale=${locale};path=/;domain=${rootDomain};max-age=${60 * 60 * 24 * 365};SameSite=Lax`
31+
32+
// Set cookie without domain for public suffixes (e.g. pages.dev, github.io)
33+
// and with root domain for custom domains (e.g. .explainer.dev)
34+
const isPublicSuffix = parts.length <= 2 || ['pages.dev', 'github.io', 'vercel.app', 'netlify.app', 'workers.dev'].some(
35+
(s) => hostname.endsWith(s)
36+
)
37+
38+
const domainAttr = isPublicSuffix ? '' : `;domain=.${parts.slice(-2).join('.')}`
39+
document.cookie = `locale=${locale};path=/${domainAttr};max-age=${60 * 60 * 24 * 365};SameSite=Lax`
3340
}
3441

3542
export function LocaleSwitcher({ locales, currentLocale, switchUrls = {}, dropUp, onLocaleChange }: LocaleSwitcherProps) {

0 commit comments

Comments
 (0)