Agregar ConsentForge a Next.js
Opción 1: Componente Script (recomendado)
Use el componente <Script> de Next.js con strategy="beforeInteractive" para asegurarse de que ConsentForge se cargue antes que cualquier otro script:
// app/layout.tsx (App Router) o 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>
)
}
Opción 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>
)
}
Bloquear scripts de terceros
Use strategy="lazyOnload" para los scripts que requieren consentimiento — no se cargarán hasta que se otorgue el consentimiento y ConsentForge los libere:
<Script
src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX"
data-consent-category="analytics"
strategy="lazyOnload"
/>
Variables de entorno
Añada a .env.local:
NEXT_PUBLIC_CONSENTFORGE_TOKEN=cf_live_your_token_here
TypeScript: tipos de window de ConsentForge
// types/consentforge.d.ts
interface Window {
ConsentForge: {
openPreferenceCenter: () => void;
getConsent: (category: string) => boolean;
on: (event: string, callback: (data: unknown) => void) => void;
}
}