diff --git a/packages/cookie-banner/package.json b/packages/cookie-banner/package.json index c07beddd1..216b2a3b3 100644 --- a/packages/cookie-banner/package.json +++ b/packages/cookie-banner/package.json @@ -3,9 +3,17 @@ "version": "0.0.0", "description": "Probo cookie consent banner SDK", "type": "module", - "main": "dist/cookie-banner.iife.js", + "main": "dist/cookie-banner.mjs", "module": "dist/cookie-banner.mjs", "types": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/cookie-banner.mjs", + "default": "./dist/cookie-banner.mjs" + }, + "./iife": "./dist/cookie-banner.iife.js" + }, "scripts": { "build": "node build.mjs && tsc --emitDeclarationOnly", "check": "tsc --noEmit" diff --git a/packages/cookie-banner/src/activation.ts b/packages/cookie-banner/src/activation.ts index 7ae70cd79..d43cad80c 100644 --- a/packages/cookie-banner/src/activation.ts +++ b/packages/cookie-banner/src/activation.ts @@ -30,9 +30,14 @@ const ACTIVATABLE_TAGS = new Set([ function activateScript(el: HTMLScriptElement): void { const replacement = document.createElement("script"); + const originalType = el.getAttribute("data-type"); for (const attr of el.attributes) { - if (attr.name === "type" || attr.name === ATTR_CATEGORY) { + if ( + attr.name === "type" || + attr.name === "data-type" || + attr.name === ATTR_CATEGORY + ) { continue; } if (attr.name === ATTR_SRC) { @@ -42,7 +47,9 @@ function activateScript(el: HTMLScriptElement): void { replacement.setAttribute(attr.name, attr.value); } - replacement.setAttribute("type", "text/javascript"); + if (originalType) { + replacement.setAttribute("type", originalType); + } replacement.setAttribute(ATTR_ACTIVATED, ""); if (el.textContent) { diff --git a/packages/cookie-banner/src/client.ts b/packages/cookie-banner/src/client.ts index cd56088d3..cfcefe5a3 100644 --- a/packages/cookie-banner/src/client.ts +++ b/packages/cookie-banner/src/client.ts @@ -233,9 +233,10 @@ export class CookieBannerClient { private activate(consentData: Record): void { activateElements(consentData); - if (!this.observer) { - this.observer = observeAndActivate(consentData); + if (this.observer) { + this.observer.disconnect(); } + this.observer = observeAndActivate(consentData); } destroy(): void { diff --git a/packages/cookie-banner/src/http.ts b/packages/cookie-banner/src/http.ts index be975b70a..c68ac03d4 100644 --- a/packages/cookie-banner/src/http.ts +++ b/packages/cookie-banner/src/http.ts @@ -22,7 +22,7 @@ import { } from "./errors"; const DEFAULT_TIMEOUT_MS = 5_000; -const MAX_RETRIES = 2; +const MAX_ATTEMPTS = 3; const BASE_DELAY_MS = 500; export interface RequestOptions { @@ -129,7 +129,7 @@ export async function fetchJSON( let lastError: Error | undefined; - for (let attempt = 0; attempt < MAX_RETRIES; attempt++) { + for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) { if (attempt > 0) { await delay(jitteredBackoff(attempt - 1)); } @@ -138,6 +138,9 @@ export async function fetchJSON( try { response = await fetchWithTimeout(url, init, timeout); } catch (err) { + if (signal?.aborted) { + throw err; + } if (err instanceof TimeoutError || err instanceof NetworkError) { lastError = err; continue;