-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.ts
53 lines (47 loc) · 1.55 KB
/
plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Vendor
import { CB_NAME, consentBannerURL, useLogger } from '@ambitiondev/cookiebot-common';
// App imports
import { defineNuxtPlugin, useRouter } from '#app';
import { useServerHead } from '#imports';
// Module Imports
import * as pluginOptions from '#cookiebot-options';
export default defineNuxtPlugin((nuxt) => {
const defaultLocale =
// @ts-expect-error - i18n is not typed in this context
'$i18n' in nuxt && 'locale' in nuxt.$i18n ? (nuxt.$i18n.locale.value as string) : undefined;
const router = useRouter();
const { error } = useLogger();
const { autoConsentBanner, cookieBotId, culture = defaultLocale, ...rest } = pluginOptions;
if (!cookieBotId?.length) {
return error('No Cookiebot ID given. Aborting...');
}
if (autoConsentBanner) {
useServerHead(
{
script: [
{
id: CB_NAME,
src: consentBannerURL({
cookieBotId,
culture,
...rest,
}),
},
],
},
{
tagPosition: 'head',
tagPriority: 'critical',
}
);
}
if (import.meta.client) {
router.afterEach(() => {
if (window instanceof Window && 'Cookiebot' in window) {
window.requestAnimationFrame(() => {
window.Cookiebot.runScripts();
});
}
});
}
});