From 6f5711b8f14233a8d0debd809c413e32008c613a Mon Sep 17 00:00:00 2001 From: sebastjanartic <45803536-sebastjanartic@users.noreply.replit.com> Date: Thu, 4 Sep 2025 07:12:29 +0000 Subject: [PATCH] Improve cookie consent banner behavior on page navigation Fix the cookie consent banner to properly check for existing consent in local storage, preventing it from showing on every page load if consent has already been granted. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/2cd2c0bc-434c-4bc9-ad3f-b99d3897a0d1/DVlzRoR --- .replit | 2 +- client/src/components/CookieConsent.tsx | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.replit b/.replit index 9eb5d4c..2d8a3dc 100644 --- a/.replit +++ b/.replit @@ -40,4 +40,4 @@ args = "npm run dev" waitForPort = 5000 [agent] -integrations = ["javascript_google_analytics==1.0.0", "javascript_database==1.0.0"] +integrations = ["javascript_database==1.0.0", "javascript_google_analytics==1.0.0"] diff --git a/client/src/components/CookieConsent.tsx b/client/src/components/CookieConsent.tsx index ce4f71c..559fcfb 100644 --- a/client/src/components/CookieConsent.tsx +++ b/client/src/components/CookieConsent.tsx @@ -18,9 +18,22 @@ export default function CookieConsent() { }); useEffect(() => { - // Clear old consent to show new banner - localStorage.removeItem('cookie_consent'); - setShowBanner(true); + // Check if user has already given consent + const savedConsent = localStorage.getItem('cookie_consent'); + if (savedConsent) { + try { + const parsedConsent = JSON.parse(savedConsent); + setConsent(parsedConsent); + updateGoogleConsent(parsedConsent); + setShowBanner(false); // Don't show banner if consent already exists + } catch (error) { + // If parsing fails, show banner + setShowBanner(true); + } + } else { + // No saved consent, show banner + setShowBanner(true); + } }, []); const updateGoogleConsent = (consentState: ConsentState) => {