diff --git a/index.d.ts b/index.d.ts index a6c594fff81f2..fc0fd4acce39f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,7 +5,7 @@ declare module "*.scss" { // dom custom event interface CustomEventMap { - nav: CustomEvent<{ url: FullSlug }> + nav: CustomEvent<{ url: FullSlug; significantLoad?: true }> themechange: CustomEvent<{ theme: "light" | "dark" }> } diff --git a/quartz/components/scripts/spa.inline.ts b/quartz/components/scripts/spa.inline.ts index df48f0403426f..9942b62b97cc5 100644 --- a/quartz/components/scripts/spa.inline.ts +++ b/quartz/components/scripts/spa.inline.ts @@ -35,8 +35,8 @@ const getOpts = ({ target }: Event): { url: URL; scroll?: boolean } | undefined return { url: new URL(href), scroll: "routerNoscroll" in a.dataset ? false : undefined } } -function notifyNav(url: FullSlug) { - const event: CustomEventMap["nav"] = new CustomEvent("nav", { detail: { url } }) +function notifyNav(url: FullSlug, significantLoad?: true) { + const event: CustomEventMap["nav"] = new CustomEvent("nav", { detail: { url, significantLoad } }) document.dispatchEvent(event) } @@ -177,7 +177,7 @@ function createRouter() { } createRouter() -notifyNav(getFullSlug(window)) +notifyNav(getFullSlug(window), true) if (!customElements.get("route-announcer")) { const attrs = { diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts index 08278305e42c8..8ca6f4fe4c593 100644 --- a/quartz/plugins/emitters/componentResources.ts +++ b/quartz/plugins/emitters/componentResources.ts @@ -127,6 +127,7 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso goatcounterScript.async = true goatcounterScript.setAttribute("data-goatcounter", "https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count") + document.addEventListener("nav", (e) => !e.detail.significantLoad && window.goatcounter?.count()) document.head.appendChild(goatcounterScript) `) } else if (cfg.analytics?.provider === "posthog") { @@ -169,7 +170,7 @@ function addGlobalPageResources(ctx: BuildCtx, componentResources: ComponentReso componentResources.afterDOMLoaded.push(` window.spaNavigate = (url, _) => window.location.assign(url) window.addCleanup = () => {} - const event = new CustomEvent("nav", { detail: { url: document.body.dataset.slug } }) + const event = new CustomEvent("nav", { detail: { url: document.body.dataset.slug, significantLoad: true } }) document.dispatchEvent(event) `) }