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
This commit is contained in:
sebastjanartic 2025-09-04 07:12:29 +00:00
parent 8b902ddf32
commit 6f5711b8f1
2 changed files with 17 additions and 4 deletions

View File

@ -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"]

View File

@ -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) => {