UNPKG

nextjs-cookie-consent

Version:

A GDPR/DSGVO-compliant cookie consent banner for Next.js

45 lines (44 loc) 1.65 kB
var CONSENT_KEY = 'nextjs-cookie-consent'; export var storageUtils = { get: function (strategy) { if (typeof window === 'undefined') return null; try { if (strategy === 'localStorage') { var stored = localStorage.getItem(CONSENT_KEY); return stored ? JSON.parse(stored) : null; } else { var cookies = document.cookie.split(';'); var consentCookie = cookies.find(function (c) { return c.trim().startsWith("".concat(CONSENT_KEY, "=")); }); if (consentCookie) { var value = consentCookie.split('=')[1]; return JSON.parse(decodeURIComponent(value)); } } } catch (error) { console.error('Error reading consent:', error); } return null; }, set: function (strategy, consent) { if (typeof window === 'undefined') return; try { var value = JSON.stringify(consent); if (strategy === 'localStorage') { localStorage.setItem(CONSENT_KEY, value); } else { // Set cookie with 1 year expiry var expires = new Date(); expires.setFullYear(expires.getFullYear() + 1); document.cookie = "".concat(CONSENT_KEY, "=").concat(encodeURIComponent(value), "; expires=").concat(expires.toUTCString(), "; path=/; SameSite=Lax"); } } catch (error) { console.error('Error saving consent:', error); } } };