Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions site/src/components/Analytics.astro
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ const enabled = Boolean(measurementId) && import.meta.env.PROD;
function readPreference() {
try {
var stored = window.localStorage.getItem(STORAGE_KEY);
return stored === "granted" || stored === "denied" ? stored : null;
if (stored === "granted" || stored === "denied") return stored;
} catch (_) {
// A locked-down browser throws on access rather than returning
// null. The in-memory answer still governs this page.
return memoryPreference;
// null.
}
// Storage had no answer, or would not give one. Either way the
// choice made on this page still governs it: a browser can let the
// read succeed and refuse the write — Safari's private mode and a
// full quota both do — and reading back null there would discard an
// explicit refusal and start measuring anyway.
return memoryPreference;
}

function writePreference(preference) {
Expand Down Expand Up @@ -335,6 +340,17 @@ const enabled = Boolean(measurementId) && import.meta.env.PROD;
},
};

// Mirrored across tabs, which this file has claimed from the start and
// nothing did. The listener above reaches only subscribers, and the
// only subscriber is the privacy page's control, which redraws its own
// text. So a reader with a docs page open who went to /privacy and
// denied kept being measured in the tab they left behind until it
// happened to reload. Applying the preference here is what makes the
// claim true.
window.addEventListener("storage", function (event) {
if (event.key === STORAGE_KEY || event.key === null) apply();
});

function ready(run) {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", run, { once: true });
Expand Down
Loading