From e5ed5697170b416d519d945c9949368c4d9738a0 Mon Sep 17 00:00:00 2001 From: stefpi <19478336+stefpi@users.noreply.github.com> Date: Tue, 1 Sep 2026 20:10:55 -0700 Subject: [PATCH 1/2] remove referral on load --- src/lib/utils/referral.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/lib/utils/referral.js b/src/lib/utils/referral.js index bb3bf3b6..a0804ac6 100644 --- a/src/lib/utils/referral.js +++ b/src/lib/utils/referral.js @@ -4,9 +4,14 @@ export const REFERRAL_QUERY_PARAM = 'ref'; export const REFERRAL_DISCOUNT = 50; export const isReferralCode = (code) => /^[a-z0-9]{7}$/i.test(code || ''); -export function getReferralCode(url = browser ? window.location.href : '') { - if (!browser || !url) return null; +export function getReferralCode() { + if (!browser) return null; - const code = new URL(url).searchParams.get(REFERRAL_QUERY_PARAM); + const referralUrl = new URL(window.location.href); + const code = referralUrl.searchParams.get(REFERRAL_QUERY_PARAM); + if (referralUrl.searchParams.has(REFERRAL_QUERY_PARAM)) { + referralUrl.searchParams.delete(REFERRAL_QUERY_PARAM); + window.history.replaceState(window.history.state, '', referralUrl); + } return isReferralCode(code) ? code : null; } From 6371fb16652cfc72e78c7dcb4623ed93d7cee925 Mon Sep 17 00:00:00 2001 From: stefpi <19478336+stefpi@users.noreply.github.com> Date: Wed, 2 Sep 2026 11:15:11 -0700 Subject: [PATCH 2/2] make better --- src/lib/utils/referral.js | 8 ++------ src/routes/+layout.svelte | 14 +++++++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/lib/utils/referral.js b/src/lib/utils/referral.js index a0804ac6..208ccfad 100644 --- a/src/lib/utils/referral.js +++ b/src/lib/utils/referral.js @@ -7,11 +7,7 @@ export const isReferralCode = (code) => /^[a-z0-9]{7}$/i.test(code || ''); export function getReferralCode() { if (!browser) return null; - const referralUrl = new URL(window.location.href); - const code = referralUrl.searchParams.get(REFERRAL_QUERY_PARAM); - if (referralUrl.searchParams.has(REFERRAL_QUERY_PARAM)) { - referralUrl.searchParams.delete(REFERRAL_QUERY_PARAM); - window.history.replaceState(window.history.state, '', referralUrl); - } + const code = new URL(window.location.href).searchParams.get(REFERRAL_QUERY_PARAM); + return isReferralCode(code) ? code : null; } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index dc04793b..fd799c10 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -7,7 +7,10 @@ import "@fontsource/inter/700.css"; import '@fontsource/jetbrains-mono/400.css'; + import { onMount } from "svelte"; import { get } from "svelte/store"; + import { page } from "$app/stores"; + import { replaceState, afterNavigate } from '$app/navigation'; import Badge from "$lib/components/Badge.svelte"; import Grid from "$lib/components/Grid.svelte"; @@ -32,9 +35,6 @@ showCart, } from "../store.js"; - import { onMount } from "svelte"; - import { page } from "$app/stores"; - let loading = false; async function openCart() { @@ -57,6 +57,14 @@ loading = false; } + afterNavigate(() => { + if (!$page.url.searchParams.has('ref')) return; + + const url = new URL($page.url); + url.searchParams.delete('ref'); + replaceState(url, $page.state); + }) + onMount(async () => { const referralCode = getReferralCode(); if (referralCode) await createCart(referralCode);