From 5417ad729873e9884ef8100e54f5be742d72d203 Mon Sep 17 00:00:00 2001 From: multipleof4 Date: Sat, 11 Oct 2025 08:56:09 -0700 Subject: [PATCH] Feat: Log analytics only if enabled via KV prefix --- functions/[slug].js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/functions/[slug].js b/functions/[slug].js index 08bc470..4d9dedc 100644 --- a/functions/[slug].js +++ b/functions/[slug].js @@ -1,18 +1,17 @@ export async function onRequestGet({ request, params, env, next, waitUntil }) { if (['abuse', 'admin', 'api', 'dash', 'acceptable-use'].includes(params.slug)) return next(); try { - const dest = await env.KV_EV.get(params.slug); - if (dest) { + let dest = await env.KV_EV.get(params.slug); + if (dest?.startsWith('✺')) { waitUntil((async () => { let ref = 'direct'; try { ref = new URL(request.headers.get('Referer')).hostname } catch {} - const stmt = ` - INSERT INTO analytics (slug, referrer, count) VALUES (?, ?, 1) - ON CONFLICT(slug, referrer) DO UPDATE SET count = count + 1; - `; + const stmt = `INSERT INTO analytics (slug, referrer, count) VALUES (?, ?, 1) ON CONFLICT(slug, referrer) DO UPDATE SET count = count + 1;`; await env.D1_EV.prepare(stmt).bind(params.slug, ref).run().catch(console.error); })()); + dest = dest.substring(1); } + const url = dest ? `https://${dest}` : new URL('/', request.url).href; return Response.redirect(url, dest ? 301 : 302); } catch (e) {