GDPR-Compliant Cookie Solution
A comprehensive self-hosted solution for managing cookies and ensuring GDPR compliance in your web applications.
Features
- 🍪 Complete Cookie Management: Control cookies across your entire site
- 🛡️ GDPR, CCPA, and ePrivacy Compliant: Meets all major privacy regulations
- 🔍 Cookie Scanner: Automatically detects and categorizes cookies
- 🚫 Script Blocker: Prevents tracking scripts from loading until consent is given
- 🌐 Multi-language Support: Easily translate to any language
- 🎨 Fully Customizable UI: Match your brand and design system
- 📊 Consent Logging: Record consent for compliance requirements
- 📱 Responsive Design: Works on all devices
- ⚡ Lightweight: Under 20KB gzipped
- 🔄 Framework Agnostic: Works with React, Next.js, Vue, and more
Installation
npm install gdpr-cookie-solution
yarn add gdpr-cookie-solution
pnpm add gdpr-cookie-solution
Quick Start
import React from "react";
import { CookieConsent, DEFAULT_CONFIG } from "gdpr-cookie-solution";
function App() {
return (
<div className="App">
{/* Your app content */}
<CookieConsent config={DEFAULT_CONFIG} />
</div>
);
}
export default App;
Configuration
The solution is highly configurable. Here's an example configuration:
const config = {
storageMethod: "cookie",
cookieName: "gdpr_consent",
cookieExpiry: 365,
privacyPolicyUrl: "/privacy-policy",
companyName: "Your Company Name",
contactEmail: "privacy@yourcompany.com",
blockedContentMessage: "Please accept cookies to view this content",
autoBlockCookies: true,
regionBased: true,
requireConsentInEU: true,
euCountryCodes: [
"AT",
"BE",
"BG",
"HR",
"CY",
"CZ",
"DK",
"EE",
"FI",
"FR",
"DE" ,
],
consentExpiryDays: 365,
defaultConsent: {
necessary: true,
functional: false,
analytics: false,
advertising: false,
uncategorized: false,
},
logConsent: true,
bannerConfig: {
position: "bottom",
layout: "bar",
rejectButtonEnabled: true,
settingsButtonEnabled: true,
acceptAllButtonLabel: "Accept All",
rejectAllButtonLabel: "Reject All",
settingsButtonLabel: "Preferences",
bannerHeading: "We Value Your Privacy",
bannerDescription:
"This website uses cookies to ensure you get the best experience on our website.",
showPoweredBy: true,
},
customStyles: {
bannerBackgroundColor: "#ffffff",
bannerTextColor: "#333333",
primaryButtonColor: "#0070f3",
primaryButtonTextColor: "#ffffff",
secondaryButtonColor: "transparent",
secondaryButtonTextColor: "#0070f3",
switchActiveColor: "#0070f3",
linkColor: "#0070f3",
borderRadius: "8px",
fontFamily: "inherit",
darkMode: false,
},
language: "en",
translations: {
en: {
consentTitle: "Cookie Consent",
},
fr: {
consentTitle: "Consentement des cookies",
},
},
};
Components
CookieConsent
The main banner component that appears when a user visits your site.
import { CookieConsent } from "gdpr-cookie-solution";
<CookieConsent
config={config}
onConsent={(accepted) => console.log("Consent given:", accepted)}
/>;
ConsentModal
A detailed modal that allows users to customize their cookie preferences.
import { ConsentModal } from "gdpr-cookie-solution";
function MyComponent() {
const [isOpen, setIsOpen] = useState(false);
return (
<>
<button onClick={() => setIsOpen(true)}>Cookie Settings</button>
<ConsentModal
config={config}
open={isOpen}
onClose={() => setIsOpen(false)}
onSave={(consent) => console.log("Saved preferences:", consent)}
/>
</>
);
}
BlockedContent
A component that only shows its children if the user has consented to the specified category.
import { BlockedContent } from "gdpr-cookie-solution";
<BlockedContent
category="analytics"
config={config}
fallback={<p>Please accept analytics cookies to view this content</p>}
onAccept={() => console.log("Analytics accepted")}
>
{/* Analytics script or component that should only load with consent */}
<script src="https://www.google-analytics.com/analytics.js" />
</BlockedContent>;
Hooks
useConsent
A hook for accessing and managing consent state.
import { useConsent } from "gdpr-cookie-solution";
function CookieManager() {
const {
consent,
bannerVisible,
modalVisible,
loading,
acceptAll,
rejectAll,
setSpecificConsent,
showPreferences,
closePreferences,
resetConsent,
} = useConsent(config);
}
useCookieScanner
A hook for scanning and categorizing cookies.
import { useCookieScanner } from "gdpr-cookie-solution";
function CookieScanner() {
const {
cookies,
loading,
getCookiesByCategory,
addCustomCookie,
rescan,
} = useCookieScanner();
}
Advanced Usage
Manual Services Configuration
For more advanced usage, you can directly use the services:
import {
CookieScanner,
CookieBlocker,
ConsentStorage,
} from "gdpr-cookie-solution";
const scanner = new CookieScanner();
const blocker = new CookieBlocker();
const storage = new ConsentStorage(config);
scanner.addKnownCookie("_myCustomCookie", "functional", "My Service");
blocker.addCategoryRule("uservoice", "functional");
blocker.init();
const savedConsent = storage.getConsent();
if (savedConsent && !storage.isConsentExpired(savedConsent)) {
blocker.applyConsent(savedConsent);
}
Server-Side Logic (Next.js Example)
import { NextRequest, NextResponse } from "next/server";
export function middleware(req: NextRequest) {
const response = NextResponse.next();
const consentCookie = req.cookies.get("gdpr_consent");
let consent = null;
if (consentCookie) {
try {
consent = JSON.parse(consentCookie.value);
} catch (e) {
}
}
if (!consent || !consent.analytics) {
response.headers.set(
"Cache-Control",
"no-store, max-age=0, must-revalidate"
);
}
return response;
}
Browser Support
This solution works on all modern browsers:
- Chrome/Edge (latest)
- Firefox (latest)
- Safari (latest)
- Opera (latest)
- Internet Explorer 11 (with polyfills)
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you need help with this library, please open an issue on GitHub.
Built with privacy in mind. Happy coding!