ConsentForge toevoegen aan Next.js
Optie 1: Script-component (aanbevolen)
Gebruik het <Script>-component van Next.js met strategy="beforeInteractive" om ervoor te zorgen dat ConsentForge laadt vóór andere scripts:
// app/layout.tsx (App Router) of pages/_document.tsx (Pages Router)
import Script from 'next/script'
export default function RootLayout({ children }) {
return (
<html>
<head>
<Script
src="https://cdn.consentforge.com/consentforge.min.js"
data-token={process.env.NEXT_PUBLIC_CONSENTFORGE_TOKEN}
strategy="beforeInteractive"
/>
</head>
<body>{children}</body>
</html>
)
}
Optie 2: _document.tsx (Pages Router)
import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html>
<Head>
<script
src="https://cdn.consentforge.com/consentforge.min.js"
data-token={process.env.NEXT_PUBLIC_CONSENTFORGE_TOKEN}
defer
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
Scripts van derden blokkeren
Gebruik strategy="lazyOnload" voor scripts waarvoor toestemming nodig is — ze worden pas geladen nadat toestemming is gegeven en ConsentForge ze vrijgeeft:
<Script
src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX"
data-consent-category="analytics"
strategy="lazyOnload"
/>
Omgevingsvariabelen
Voeg toe aan .env.local:
NEXT_PUBLIC_CONSENTFORGE_TOKEN=cf_live_uw_token_hier
TypeScript: ConsentForge window-typen
// types/consentforge.d.ts
interface Window {
ConsentForge: {
openPreferenceCenter: () => void;
getConsent: (category: string) => boolean;
on: (event: string, callback: (data: unknown) => void) => void;
}
}