Skip to content

Commit f59b1b2

Browse files
authored
Use parent domain to set gitbook visitor cookie (#814)
* Use parent domain to set gitbook visitor cookie * changeset
1 parent 356939d commit f59b1b2

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

.changeset/red-kings-sniff.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/adaptive': patch
3+
---
4+
5+
Use parent domain to set gitbook visitor cookie

packages/adaptive/src/bucket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function withBucket(client: BucketClient): () => void {
1616
features[key] = enabled;
1717
}
1818

19-
writeGitBookVisitorCookie('bucket', features);
19+
writeGitBookVisitorCookie('bucket', { bucket: features });
2020
};
2121

2222
// Determine if we're in a browser environment

packages/adaptive/src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,19 @@ export function writeGitBookVisitorCookie(name: string, value: unknown) {
1818

1919
Cookies.set(COOKIE_NAME, COOKIE_VALUE, {
2020
secure: true,
21+
domain: `.${getParentDomain()}`,
2122
});
2223
}
24+
25+
function getParentDomain(): string {
26+
const hostname = window.location.hostname;
27+
const parts = hostname.split('.');
28+
// If it's a subdomain (has at least 3 parts, like 'blog.example.com')
29+
if (parts.length > 2) {
30+
// Return the parent domain (last 2 parts)
31+
return parts.slice(-2).join('.');
32+
}
33+
34+
// Already a top-level domain
35+
return hostname;
36+
}

0 commit comments

Comments
 (0)