Skip to content

Commit ef838bb

Browse files
Fixes Clarity
1 parent 5db2fe1 commit ef838bb

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/layouts/Layout.astro

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,30 @@ const ogImage = image ? new URL(image, Astro.site ?? 'https://devproxy.net').hre
6464
Clarity.init("vr3ajkmgm3");
6565
}
6666

67-
function checkConsent() {
68-
if (w.astroConsent) {
69-
const consent = w.astroConsent.get();
70-
if (consent?.categories?.analytics) loadClarity();
71-
}
72-
}
67+
function setup() {
68+
if (!w.astroConsent) return;
7369

74-
// Check on load (returning visitor who already accepted)
75-
checkConsent();
70+
// Check if returning visitor already consented
71+
const consent = w.astroConsent.get();
72+
if (consent?.categories?.analytics) loadClarity();
7673

77-
// Wrap astroConsent.set to detect consent changes without a page reload
78-
if (w.astroConsent) {
74+
// Wrap astroConsent.set to detect consent changes without a page reload
7975
const originalSet = w.astroConsent.set.bind(w.astroConsent);
8076
w.astroConsent.set = (categories: Record<string, boolean>) => {
8177
originalSet(categories);
8278
if (categories.analytics) loadClarity();
8379
};
8480
}
8581

82+
// This inline module script runs before the external astro-consent module
83+
// that creates window.astroConsent. Defer setup to the next macrotask so
84+
// all deferred module scripts have executed first.
85+
if (w.astroConsent) {
86+
setup();
87+
} else {
88+
setTimeout(setup, 0);
89+
}
90+
8691
// Rename "Reject" button to "Essential only" (not configurable in astro-consent)
8792
const rejectBtn = document.querySelector<HTMLButtonElement>('#astro-consent-banner .cb-reject');
8893
if (rejectBtn) {

0 commit comments

Comments
 (0)