#2574·quartz

GoatCounter analytics counts the landing page twice when count.js is cached

Author: RuizSerraCreated Sep 27, 2026Updated Sep 27, 2026

Describe the bug

With analytics.provider: goatcounter, the page a visitor lands on is sometimes counted twice. Whether it is depends on which finishes first: count.js loading, or the router dispatching the initial nav event.

The GoatCounter script in quartz/plugins/emitters/componentResources.ts counts once in count.js's onload, and registers the nav listener inside that same onload:

javascript
goatcounterScript.onload = () => {
  window.goatcounter.endpoint = endpoint;
  goatcounter.count({ path: location.pathname });
  document.addEventListener('nav', () => {
    goatcounter.count({ path: location.pathname });
  });
};

That is only correct if onload fires after the initial nav. While all afterDOMLoaded scripts were bundled into one postscript, it did: the router dispatched the initial nav synchronously, before any onload could run. Since dfde22a ("emit plugin scripts individually instead of bundling into postscript"), production builds import the component scripts in parallel, await them, and only then import the router. If count.js is in the HTTP cache, its onload can fire in that gap, so the listener is already registered when the initial nav arrives and the landing page is counted twice.

The cached case is the common one: gc.zgo.at/count.js is served with cache-control: max-age=25600000 (~296 days), so it applies to nearly every returning visitor.

To Reproduce

  1. Set analytics: { provider: goatcounter, websiteId: <anything> } and run npx quartz build. Not --serve: serve mode still bundles the scripts into one IIFE in order, so the race does not occur there.
  2. Serve public/ from localhost with any static server. count.js refuses to count on localhost and logs goatcounter: not counting because of: localhost once per count() call, which makes a convenient counter.
  3. Load a page once so count.js is cached, then open a page in a fresh tab.
  4. The console intermittently shows that warning twice for the single page load.

Expected behavior

Exactly one count per page view, whichever finishes first.

Suggested fix

Register the nav listener synchronously, before count.js loads, and count only on nav events, queueing any that arrive before count.js is ready. Every page view, the first included, then produces exactly one count in either order:

diff
+      // Count once per nav event, the initial one included. The router fires the
+      // first nav after this script runs, but count.js can finish loading before
+      // or after it (before, when cached), so counting in onload as well would
+      // count the landing page twice. Navs that arrive early wait for the load.
       const endpoint = "https://${cfg.analytics.websiteId}.${cfg.analytics.host ?? "goatcounter.com"}/count";
+      let goatcounterLoaded = false;
+      let pendingPaths = [];
+      document.addEventListener('nav', () => {
+        if (goatcounterLoaded) goatcounter.count({ path: location.pathname });
+        else pendingPaths.push(location.pathname);
+      });
+
       const goatcounterScript = document.createElement('script');
       goatcounterScript.src = "${cfg.analytics.scriptSrc ?? "https://gc.zgo.at/count.js"}";
       goatcounterScript.defer = true;
       goatcounterScript.setAttribute('data-goatcounter', endpoint);
       goatcounterScript.onload = () => {
         window.goatcounter.endpoint = endpoint;
-        goatcounter.count({ path: location.pathname });
-        document.addEventListener('nav', () => {
-          goatcounter.count({ path: location.pathname });
-        });
+        goatcounterLoaded = true;
+        pendingPaths.forEach((path) => goatcounter.count({ path }));
+        pendingPaths = [];
       };

This relies on an initial nav always being dispatched after the analytics script runs. It is, in both SPA and non-SPA mode, since that dispatch is always the last afterDOMLoaded script. With this change, a production build with count.js cached logs exactly one count for the landing page and one per SPA navigation after it.

Other providers

The Google and Plausible integrations use the same pattern (first page view in onload, nav listener registered inside onload), so they likely have the same race. I have only verified GoatCounter.

Desktop (please complete the following information):

  • Quartz Version: v5.0.0 (the GoatCounter code is unchanged on current v5)
  • node Version: v24.5.0
  • npm version: 11.5.1
  • OS: macOS 26.5.1
  • Browser: Chromium-based, with enableSPA: true