Skip to content

Commit 5aedcf9

Browse files
mikewheatonConvex, Inc.
authored and
Convex, Inc.
committed
Docs: Cookie banner fix (#34163)
Fixes a bug with the cookie banner. GitOrigin-RevId: ab6ec931d34683e7374788656c58962f78afc6dc
1 parent bfbe8ce commit 5aedcf9

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

npm-packages/docs/src/components/Analytics/CookieBanner.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
import useIsBrowser from "@docusaurus/useIsBrowser";
12
import { useAnalyticsCookies } from "@site/src/components/Analytics/useAnalyticsCookies";
2-
import React, { useEffect, useState } from "react";
3+
import React from "react";
34

45
export default function CookieBanner() {
6+
const isBrowser = useIsBrowser();
57
const { allowsCookies, setAllowsCookies } = useAnalyticsCookies();
6-
const [isDeployPreview, setIsDeployPreview] = useState(false);
78

8-
useEffect(() => {
9-
setIsDeployPreview(window.location.hostname.includes("netlify.app"));
10-
}, []);
9+
// Don't render during SSR.
10+
if (!isBrowser) {
11+
return null;
12+
}
1113

12-
if (isDeployPreview || allowsCookies !== undefined) {
14+
// Don't render if the user has accepted or rejected previously.
15+
if (allowsCookies !== undefined) {
1316
return null;
1417
}
1518

npm-packages/docs/src/components/Analytics/PostHog.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function PostHog() {
2323
// write-only and PostHog says is safe to use in public apps.
2424
const key = siteConfig.customFields.POST_HOG_KEY as string;
2525
const api_host = siteConfig.customFields.POST_HOG_HOST as string;
26+
// Note that this is a production build, which includes deploy previews.
2627
const isProduction = siteConfig.customFields.NODE_ENV === "production";
2728

2829
if (!isProduction || !key || !api_host) {

npm-packages/docs/src/components/Analytics/useAnalyticsCookies.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
21
import { useCookies } from "react-cookie";
2+
import useIsBrowser from "@docusaurus/useIsBrowser";
33

44
// Ensure this is the same for the website, dashboard, Stack, etc. as they all
55
// share the same cookie. This allows a user to consent once across all of the
@@ -8,20 +8,20 @@ const COOKIE_NAME = "allowsCookies";
88

99
export function useAnalyticsCookies() {
1010
const [cookies, setCookie] = useCookies([COOKIE_NAME]);
11-
const { siteConfig } = useDocusaurusContext();
12-
13-
const isProduction = siteConfig.customFields.NODE_ENV === "production";
11+
const isBrowser = useIsBrowser();
1412

1513
// An undefined value indicates that the cookie is not present, so the user
1614
// has not yet accepted or rejected the cookie banner.
1715
const allowsCookies = cookies[COOKIE_NAME];
1816

1917
const setAllowsCookies = (value: boolean) => {
20-
setCookie(COOKIE_NAME, value, {
21-
domain: isProduction ? ".convex.dev" : undefined,
22-
path: "/",
23-
maxAge: 34560000,
24-
});
18+
if (isBrowser) {
19+
setCookie(COOKIE_NAME, value, {
20+
domain: `.${window.location.hostname}`,
21+
path: "/",
22+
maxAge: 34560000,
23+
});
24+
}
2525
};
2626

2727
return { allowsCookies, setAllowsCookies };

0 commit comments

Comments
 (0)