Repository navigation
feat(dashboard): share cookie choice across nhost.io and dashboard - #5352
Merged
Merged
Conversation
Review issue: Review finding 01 — .nhost-code/review/PR_fix-cookie-consent-analytics_COMMENT_001.md — ADDRESSED, ACCEPT Review issue: Review finding 02 — .nhost-code/review/PR_fix-cookie-consent-analytics_COMMENT_002.md — ADDRESSED, ACCEPT Resolution: - resetAnalytics() in both the dashboard and landing now calls only the loaded instance's synchronous reset() (analytics.instance?.reset()) and never waits on the buffered AnalyticsBrowser.reset(), so the "Cookie settings" reset completes even when Segment failed to load. - Both resetCookieConsent() helpers catch and log SDK reset errors and always delete the consent cookie and reload in a finally block, so a hanging or throwing SDK can no longer strand consent withdrawal. - Added matching regression tests in both apps for pending/rejecting buffered resets and a throwing instance reset. Why: - A buffered AnalyticsBrowser call only settles after a successful load; when the CDN settings fetch is blocked the SDK swallows the error and the call never settles, hanging the reset. Analytics#reset() is synchronous in the installed SDK, so calling it on analytics.instance avoids the hang without timers. Decisions: - Made resetAnalytics() and both resetCookieConsent() helpers synchronous; their only callers are onClick handlers. Preserved loaded, getAnonId, and consent cookie read behavior. Switched affected suites to vi.resetAllMocks() so unused mockImplementationOnce queues do not leak between tests. Checks: - `biome check` (dashboard, 4 files) — exit 0 - `tsc --noEmit` (dashboard) — exit 0 - `vitest run` (dashboard targeted) — exit 0: 32/32 passed - `vitest run` (landing targeted) — exit 0: 33/33 passed - `prettier --check` (landing, 4 files) — exit 0 - `tsc --noEmit` (landing) — exit 0 - `eslint .` (landing) — exit 2: cannot start; pre-existing ajv/env crash that reproduces on untouched files, WU-001 changes no package/lock (operator approved committing)
Review issue: Review finding 03 — .nhost-code/review/PR_fix-cookie-consent-analytics_COMMENT_003.md — ADDRESSED, ACCEPT Resolution: - migrateLegacyConsent() now writes the shared nhost-cookie-consent cookie only for a legacy rejection (localStorage cookie-consent='false'); a legacy acceptance is dropped while the legacy key is still removed, so the new wider-disclosure banner is shown again. Documented the decision in the function comment and updated the hook test to assert null consent, no shared cookie, and a removed legacy key. Why: - A legacy acceptance came from the old signup-only banner with narrower wording. Migrating it would write a shared Domain=nhost.io accepted cookie that loads Segment and Google Ads on nhost.io and across the dashboard without the user seeing the new banner. Carrying over only rejections never widens consent. Decisions: - Applied the operator's rejection-only migration decision (operator-approved) without changing cookie attributes or landing behavior. Checks: - `biome check` (dashboard, 2 files) — exit 0 - `tsc --noEmit` (dashboard) — exit 0 - `vitest run` (dashboard targeted) — exit 0: 32/32 passed
Contributor
Vercel Deployment Info - landing
|
Contributor
Vercel Deployment Info - dashboard
|
skoveit
approved these changes
Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goal
Both Nhost web properties — the marketing site
nhost.ioand the dashboard atapp.nhost.io— loaded Segment (and the Google Ads tag Segment pulls in), and the marketing site also loaded Plausible, in production before the visitor had made any cookie choice. The cookie banner existed but did not actually gate loading, and the two properties did not share a single decision. This change defers all Segment loading until the user explicitly accepts cookies, stores that one accept/reject decision in a single first-party cookie scoped tonhost.ioso it is shared across both properties, clears persisted Segment identifiers when the user rejects or resets consent, and removes the Plausible integration from the marketing site.Type: Enhancement
Changes
dashboard/src/lib/segment.tsandlanding/src/lib/segment.tsnow export an unloadednew AnalyticsBrowser()that buffers calls in memory and sends nothing. A new idempotentloadAnalytics()callsanalytics.load(...)exactly once (guarded by a module-levelloadedflag) and is invoked only after consent. Thedisableflag still restricts real delivery to production (dashboard:isAnalyticsEnabled()=isPlatform() && !isDevOrStaging(); landing:NEXT_PUBLIC_VERCEL_ENV === 'production'), so outside production Segment loads as a no-op but the banner still works.dashboard/.../CookieConsent/consentCookie.tsandlanding/src/lib/cookieConsent.ts(intentionally duplicated logic, each pointing at the other) read/write/delete anhost-cookie-consentcookie holdingaccepted/rejected, with a 6-monthMax-Age,SameSite=Lax,Secureon https, andDomain=nhost.iowhen the host isnhost.ioor a subdomain.getCookieDomain()returnsundefinedon localhost/preview hosts so the cookie stays host-only there. Scoping to the apex domain is what lets the dashboard and marketing site read one choice.useCookieConsentreads the cookie on the client (state isundefineduntil read, keeping SSR/hydration consistent) and returns[consent, setConsent]. The dashboard variant adds cross-component synchronization through a customnhost-cookie-consent-changewindow event and migrates a legacycookie-consentlocalStorage value into the cookie; it also clears analytics storage whenever the resolved choice isfalseornull(i.e. rejected, or reset on the other property).clearAnalyticsStorage()synchronously removes the Segmentajs_*keys fromlocalStorageand expires theajs_*cookies on both the current host andnhost.io, without loading or awaiting Segment.resetAnalytics()clears storage and, only if Segment was already loaded, awaitsanalytics.reset()and clears again.resetCookieConsent(wired into the dashboard account menu and the landing footer as a "Cookie settings" control) runsresetAnalytics(), deletes the consent cookie, and reloads so the banner reappears.signup.tsxinto_app.tsx, rendered globally whenisPlatform()withloadAnalyticsas itsonAccept;signup.tsxnow only initializes Google Ads reactively once consent istrueand analytics is enabled. The dashboardCookieConsent.tsxderives visibility from the hook (hidden once the choice is non-null) and links to the cookies policy. The landing site gains a newCookieConsent.tsx(Accept/Reject) rendered by theAnalyticscomponent.next-plausibledependency andPlausibleProviderfromlanding/.../_app.tsx;Analyticsis now always rendered (banner everywhere, Segment only loads in production) and theBlogListingdoc comment is updated accordingly.getAnonIdguard — dashboardgetAnonId()returnsundefinedwhen Segment has not been loaded, instead of awaiting an unloaded instance.Flow
flowchart TD A[Page load] --> B{nhost-cookie-consent cookie} B -- accepted --> C[loadAnalytics: Segment loads, buffered calls flush] B -- rejected/null --> D[clearAnalyticsStorage: drop ajs_* keys] B -- no choice --> E[Show cookie banner] E -- Accept --> F[writeConsentCookie accepted + loadAnalytics] E -- Reject --> G[writeConsentCookie rejected + clearAnalyticsStorage] H[Cookie settings control] --> I[resetAnalytics + deleteConsentCookie + reload]Key files
dashboard/src/lib/segment.ts(+80/-8),landing/src/lib/segment.ts(+69/-5) — deferred load, storage clearing, reset.dashboard/src/components/common/CookieConsent/consentCookie.ts(+77),landing/src/lib/cookieConsent.ts(+52) — shared cookie contract (duplicated).dashboard/.../CookieConsent/useCookieConsent.ts(+54),landing/src/components/analytics/useCookieConsent.ts(+45) — consent hook and reset helper.dashboard/src/pages/_app.tsx,dashboard/src/pages/signup.tsx,landing/src/pages/_app.tsx,landing/src/components/analytics/analytics.tsx— banner placement and consent-gated loading.landing/src/components/analytics/CookieConsent.tsx(+34), dashboardCookieConsent.tsx/AccountMenuActions.tsx, landingFooter.tsx— banner UI and "Cookie settings" entry points.landing/package.json,landing/.../BlogListing.tsx— Plausible removal and comment update.*/segment.test.ts,*/analyticsStorage.test.ts,*/cookieConsent.test.ts,*/useCookieConsent.test.ts.