@te-digital/lgpd-consent-next
v0.2.0
Published
Next.js App Router adapter for @te-digital/lgpd-consent. Server-side consent reading.
Maintainers
Readme
@te-digital/lgpd-consent-next
Next.js App Router adapter for @te-digital/lgpd-consent. Server-side consent reading + Google Consent Mode v2 boot script.
Install
pnpm add @te-digital/lgpd-consent @te-digital/lgpd-consent-react @te-digital/lgpd-consent-nextUsage
1. Boot Google Consent Mode v2 in root layout
// app/layout.tsx
import { ConsentScript } from '@te-digital/lgpd-consent-next';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html>
<head>
<ConsentScript region="BR" />
</head>
<body>{children}</body>
</html>
);
}This runs BEFORE any tracker, setting all consent states to denied. Required for GCM v2 compliance.
2. Read consent on the server
Useful for conditionally injecting tracker scripts only when the user has consented, avoiding the client-side flash.
import { readConsent } from '@te-digital/lgpd-consent-next/server';
export default async function RootLayout({ children }: { children: React.ReactNode }) {
const consent = await readConsent({ currentPolicyVersion: 1 });
return (
<html>
<body>
{consent.preferences.analytics && <ClarityScript />}
{consent.preferences.marketing && <MetaPixel />}
{children}
</body>
</html>
);
}Requires the manager configured with storage: 'cookie' (or 'auto' falling back to cookies).
API
<ConsentScript region?, defaultAnalytics?, defaultMarketing? />
Inline script tag (strategy="beforeInteractive") emitting the GCM v2 default state.
readConsent(options?): Promise<ServerConsent>
Server-side reader.
type ServerConsent = {
status: 'pending' | 'granted' | 'expired';
preferences: ConsentPreferences;
acceptedAt: string | null;
policyVersion: number | null;
};License
MIT © T&E Digital
