Avoid saving same-domain referrers and resolve path-only referrers correctly

This commit is contained in:
Mike Cao committed 2026-08-03 23:03:52 -07:00
1 parent c5d34d10ad
commit 8efd5d7279
4 files changed
+131 -6

No files matched your search

+18 -2
View File
@@ -192,7 +192,7 @@ export async function POST(request: Request) {
let urlPath =
currentUrl.pathname === '/undefined' ? '' : currentUrl.pathname + currentUrl.hash;
const urlQuery = currentUrl.search.substring(1);
const urlDomain = currentUrl.hostname.replace(/^www./, '');
const urlDomain = currentUrl.hostname.replace(/^www\./, '');
let referrerPath: string;
let referrerQuery: string;
@@ -218,11 +218,27 @@ export async function POST(request: Request) {
}
if (referrer) {
const referrerUrl = new URL(referrer, base);
// Canonicalize the event domain (lowercase, punycode, no port) so it
// compares correctly against the parsed referrer hostname
let eventDomain = urlDomain;
if (hostname) {
try {
eventDomain = new URL(`https://${hostname}`).hostname.replace(/^www\./, '');
} catch {
eventDomain = hostname.replace(/^www\./, '');
}
}
// Resolve path-only referrers against the event's domain, not the localhost fallback
const referrerUrl = new URL(referrer, eventDomain ? `https://${eventDomain}` : base);
referrerPath = referrerUrl.pathname;
referrerQuery = referrerUrl.search.substring(1);
referrerDomain = referrerUrl.hostname.replace(/^www\./, '');
// Never save the referrer domain for self-referrals
if (referrerDomain === eventDomain) {
referrerDomain = undefined;
}
}
const eventType = linkId