Added healhtRoute, honeypot, ConsentScript, asserEnv

This commit is contained in:
2026-10-10 00:17:13 +02:00
parent 52977e67fc
commit 1b52060dce
63 changed files with 2110 additions and 36 deletions
+109
View File
@@ -0,0 +1,109 @@
'use client'
import { useEffect, useRef } from 'react'
import type { ConsentCategory } from './categories.js'
import { useConsentContext } from './ConsentContext.js'
type ConsentScriptProps = {
/** Default true. */
async?: boolean
/** Extra attributes on the <script> (e.g. { 'data-domain': 'x.com' }). */
attributes?: Record<string, string>
/**
* Consent category that must be granted before the script loads. Typically
* 'analytics' (GA, Plausible) or 'marketing' (Meta Pixel, ads, chat widgets).
*/
category: ConsentCategory
/**
* Inline JS to run once granted. If `src` is also set, it runs after the
* external script loads (so init code can rely on the library). A plain
* string of code — NOT user input (you control it), it goes into a <script>.
*/
code?: string
defer?: boolean
/** Stable id used to dedupe — the script is injected at most once. */
id?: string
/** External script URL. Injected only once the category is granted. */
src?: string
}
/**
* Loads a third-party script ONLY after the visitor has granted consent for its
* category — the GDPR-correct default for anything that isn't strictly
* necessary. Renders nothing until then. Reacts live to the consent context, so
* a script appears the moment the visitor accepts in the banner, with no reload.
*
* Use this for third parties that have NO Google Consent Mode of their own (Meta
* Pixel, LinkedIn Insight, chat widgets, custom analytics). GA/GTM are better
* handled by Consent Mode (the module's updateConsent already does that) — reach
* for ConsentScript when a vendor can't be gated that way.
*
* Must be rendered inside <ConsentProvider>.
*
* <ConsentScript category="marketing" src="https://connect.facebook.net/en_US/fbevents.js" />
* <ConsentScript category="analytics"
* src="https://plausible.io/js/script.js"
* attributes={{ 'data-domain': 'example.com' }} />
* <ConsentScript category="marketing" code={`fbq('init','123');fbq('track','PageView')`} />
*
* Note on withdrawal: once a script has loaded it keeps running for that page
* (a <script> tag can't be truly unloaded) — withdrawing consent stops the NEXT
* load. For vendors that support it, prefer Consent Mode so running tags honor
* the change immediately; ConsentScript gates the initial load.
*/
export function ConsentScript({
id,
async: asyncAttr = true,
attributes,
category,
code,
defer,
src,
}: ConsentScriptProps) {
const { state } = useConsentContext()
const granted = state[category] === true
const injectedRef = useRef(false)
useEffect(() => {
if (!granted || injectedRef.current) {return}
if (!src && !code) {return}
const dedupeId = id ?? (src ? `ipal-cs-${src}` : undefined)
if (dedupeId && document.getElementById(dedupeId)) {
injectedRef.current = true
return
}
injectedRef.current = true
const runInline = () => {
if (!code) {return}
const inline = document.createElement('script')
if (dedupeId) {inline.id = `${dedupeId}-inline`}
inline.textContent = code
document.head.appendChild(inline)
}
if (src) {
const el = document.createElement('script')
el.src = src
if (dedupeId) {el.id = dedupeId}
if (asyncAttr) {el.async = true}
if (defer) {el.defer = true}
if (attributes) {for (const [k, v] of Object.entries(attributes)) {el.setAttribute(k, v)}}
el.addEventListener('load', runInline, { once: true })
document.head.appendChild(el)
} else {
runInline()
}
// Deliberately no removal on cleanup: a loaded third-party script can't be
// meaningfully unloaded. Withdrawal is handled by not re-injecting (and, for
// vendors that support it, Consent Mode). See the component doc.
}, [granted, src, code, id, attributes, asyncAttr, defer])
return null
}
export default ConsentScript
+1
View File
@@ -1,5 +1,6 @@
'use client'
export { ConsentProvider, useConsentContext } from './ConsentContext.js'
export { ConsentScript } from './ConsentScript.js'
export { CookieBanner } from './CookieBanner.js'
export type { CookieBannerClassNames } from './CookieBanner.js'
export { CookieButton } from './CookieButton.js'