The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Index only link keys
Rafi212f3eab8c30
diff --git a/.gitignore b/.gitignoreindex 898bef4966..64759ecd64 100644--- a/.gitignore+++ b/.gitignore@@ -50,4 +50,5 @@ packages/stripe-app/.build/*playwright-report/**/playwright/.auth/test-results/-blob-report/\ No newline at end of file+blob-report/+.pnpm-storediff --git a/.pnpm-store/v11/index.db b/.pnpm-store/v11/index.dbdeleted file mode 100644index 4d0f4d75c0..0000000000Binary files a/.pnpm-store/v11/index.db and /dev/null differdiff --git a/.pnpm-store/v11/index.db-shm b/.pnpm-store/v11/index.db-shmdeleted file mode 100644index adf81a5561..0000000000Binary files a/.pnpm-store/v11/index.db-shm and /dev/null differdiff --git a/.pnpm-store/v11/index.db-wal b/.pnpm-store/v11/index.db-waldeleted file mode 100644index 60bb626bba..0000000000Binary files a/.pnpm-store/v11/index.db-wal and /dev/null differdiff --git a/apps/web/lib/api/partners/get-partners-count.ts b/apps/web/lib/api/partners/get-partners-count.tsindex 49ff55fd71..171073316c 100644--- a/apps/web/lib/api/partners/get-partners-count.ts+++ b/apps/web/lib/api/partners/get-partners-count.ts@@ -275,9 +275,6 @@ export async function getPartnersCount<T>(searchProvider &&candidateQuery &&candidateResult &&- // An exact lookup already names its hits; the text aggregation would count- // every document sharing a token with the pasted link instead.- !candidateResult.exact &&!databaseOnlyFilters) {try {diff --git a/apps/web/lib/api/partners/search/find-candidates.ts b/apps/web/lib/api/partners/search/find-candidates.tsindex 2e94c39f51..0671a7619b 100644--- a/apps/web/lib/api/partners/search/find-candidates.ts+++ b/apps/web/lib/api/partners/search/find-candidates.ts@@ -6,58 +6,6 @@ import type {PartnerSearchResult,} from "./types";-/**- * A pasted short link: a host with a path, with or without the protocol, e.g.- * `go.acme.com/partnername`. A bare domain is not one, because typing a domain- * is a text search, while a host plus path only ever comes from pasting a link.- */-const SHORT_LINK_QUERY_PATTERN =- /^(?:https?:\/\/)?([a-z0-9-]+(?:\.[a-z0-9-]+)+)(\/\S+)$/i;--/**- * Resolves a pasted short link to its enrollment through `Link.shortLink`,- * which is unique and indexed, so the paste matches exactly the partner who- * owns that link. The domain tokens of a short link are shared by every- * partner in the program, so the ranked path cannot make a paste precise.- *- * Misses fall through to the ranked search: a partial paste still matches the- * key by prefix. A link from another program returns no hits rather than- * falling through, since its tokens would only flood the ranked results.- */-async function findExactShortLinkResult({- programId,- query,-}: PartnerSearchCandidateQuery): Promise<PartnerSearchResult | null> {- const match = query.trim().match(SHORT_LINK_QUERY_PATTERN);-- if (!match) {- return null;- }-- const [, host, path] = match;- const link = await prisma.link.findUnique({- where: { shortLink: `https://${host}${path}` },- select: { programId: true, partnerId: true },- });-- if (!link?.partnerId || !link.programId) {- return null;- }-- if (link.programId !== programId) {- return { hits: [], exact: true };- }-- const enrollment = await prisma.programEnrollment.findUnique({- where: {- partnerId_programId: { partnerId: link.partnerId, programId },- },- select: { id: true },- });-- return { hits: enrollment ? [{ id: enrollment.id }] : [], exact: true };-}-interface FindPartnerSearchCandidatesOptions {/*** Surface provider failures instead of degrading. The benchmark needs this so@@ -94,12 +42,6 @@ export async function findPartnerSearchCandidates(}}- const exactShortLinkResult = await findExactShortLinkResult(query);-- if (exactShortLinkResult) {- return exactShortLinkResult;- }-try {return await searchProvider.searchCandidates(query);} catch (error) {diff --git a/apps/web/lib/api/partners/search/providers/turbopuffer.ts b/apps/web/lib/api/partners/search/providers/turbopuffer.tsindex 7c94aa01c0..34a20d4d53 100644--- a/apps/web/lib/api/partners/search/providers/turbopuffer.ts+++ b/apps/web/lib/api/partners/search/providers/turbopuffer.ts@@ -31,7 +31,7 @@ import { getEmailNgrams, getQueryNgrams } from "./shared";* existing namespace. A new version is backfilled alongside the old one and* swapped in, rather than rebuilt in place.*/-export const PARTNER_SEARCH_NAMESPACE = "partner-search-v5";+export const PARTNER_SEARCH_NAMESPACE = "partner-search-v4";const WRITE_BATCH_SIZE = 500;/**diff --git a/apps/web/lib/api/partners/search/searchable-values.ts b/apps/web/lib/api/partners/search/searchable-values.tsindex 131dd9ef73..8510bdb93d 100644--- a/apps/web/lib/api/partners/search/searchable-values.ts+++ b/apps/web/lib/api/partners/search/searchable-values.ts@@ -30,7 +30,6 @@ export function getPartnerSearchableValues(...document.platformTypes,...document.platformIdentifiers,...document.linkKeys,- ...document.destinationUrls,].filter((value): value is string => Boolean(value));}diff --git a/apps/web/lib/api/partners/search/serialize-document.ts b/apps/web/lib/api/partners/search/serialize-document.tsindex 8846fb76bd..949939188a 100644--- a/apps/web/lib/api/partners/search/serialize-document.ts+++ b/apps/web/lib/api/partners/search/serialize-document.ts@@ -7,13 +7,6 @@ export const partnerSearchDocumentSelect = {partnerId: true,status: true,groupId: true,- // The program's destination host, so the serializer can drop it from- // destination URLs below.- program: {- select: {- url: true,- },- },partner: {select: {name: true,@@ -40,7 +33,6 @@ export const partnerSearchDocumentSelect = {links: {select: {key: true,- url: true,},},} satisfies Prisma.ProgramEnrollmentSelect;@@ -53,46 +45,10 @@ function unique<T>(values: T[]): T[] {return Array.from(new Set(values));}-function getHost(url: string | null): string | null {- if (!url) {- return null;- }-- try {- return new URL(url).hostname.replace(/^www\./, "");- } catch {- return null;- }-}--/**- * The distinctive part of a destination URL. Query strings and fragments are- * stripped so tracking parameters never reach the index, and the host is- * dropped when it is the program's own: measured in production, ~99% of- * destination hosts in a program are its one domain, so indexing it floods- * every search containing a domain token. A host that differs (a partner's own- * site) identifies the partner and is kept.- */-function sanitizeDestinationUrl(- url: string,- programHost: string | null,-): string | null {- try {- const parsed = new URL(url);- const host = parsed.hostname.replace(/^www\./, "");- const path = parsed.pathname === "/" ? "" : parsed.pathname;-- return `${host === programHost ? "" : host}${path}` || null;- } catch {- return url;- }-}-export function serializePartnerSearchDocument(enrollment: PartnerSearchDocumentSource,): PartnerSearchDocument {- const { partner, links, program } = enrollment;- const programHost = getHost(program.url);+ const { partner, links } = enrollment;return {id: enrollment.id,@@ -107,12 +63,6 @@ export function serializePartnerSearchDocument(partner.platforms.map(({ identifier }) => identifier),),linkKeys: unique(links.map(({ key }) => key)),- destinationUrls: unique(- links.flatMap(({ url }) => {- const sanitized = sanitizeDestinationUrl(url, programHost);- return sanitized ? [sanitized] : [];- }),- ),status: enrollment.status,groupId: enrollment.groupId,country: partner.country,diff --git a/apps/web/lib/api/partners/search/types.ts b/apps/web/lib/api/partners/search/types.tsindex c7fc56fe3a..55b9d4b9c0 100644--- a/apps/web/lib/api/partners/search/types.ts+++ b/apps/web/lib/api/partners/search/types.ts@@ -53,12 +53,10 @@ export interface PartnerSearchDocument {platformTypes: PlatformType[];platformIdentifiers: string[];- // Searchable link fields. Short-link domains are absent: a program's- // partners share one, so it never narrows a search. A full short link is an- // exact database lookup instead. Destination URLs are stored sanitized (see- // serialize-document).+ // The only searchable link field. Domains and destination URLs are absent:+ // a program's partners share them, so they never narrow a search, and+ // partner-specific sites are already indexed via the website platform.linkKeys: string[];- destinationUrls: string[];// Filterable fields. Metrics are deliberately absent: they move on every click// and conversion, so indexing them would make the document churn continuously.@@ -75,12 +73,6 @@ export interface PartnerSearchHit {export interface PartnerSearchResult {hits: PartnerSearchHit[];- /**- * The hits are an exact lookup rather than ranked matches, so the count must- * come from them and never from the text aggregation, which would count- * every document sharing a token with the pasted value.- */- exact?: boolean;}/**diff --git a/apps/web/scripts/dev/benchmark-partner-search.ts b/apps/web/scripts/dev/benchmark-partner-search.tsindex 642896594b..e29151e2df 100644--- a/apps/web/scripts/dev/benchmark-partner-search.ts+++ b/apps/web/scripts/dev/benchmark-partner-search.ts@@ -217,7 +217,6 @@ function createSearchCases(document: PartnerSearchDocument): SearchCase[] {const platformType = document.platformTypes[0];const platformIdentifier = document.platformIdentifiers[0];const linkKey = document.linkKeys[0];- const destinationUrl = document.destinationUrls[0];if (!document.email ||@@ -225,8 +224,7 @@ function createSearchCases(document: PartnerSearchDocument): SearchCase[] {!document.description ||!platformType ||!platformIdentifier ||- !linkKey ||- !destinationUrl+ !linkKey) {throw new Error("The benchmark sample must have an email, company, description, platform, and link.",@@ -244,10 +242,6 @@ function createSearchCases(document: PartnerSearchDocument): SearchCase[] {query: longestSearchToken(platformIdentifier),},{ field: "link key", query: linkKey },- {- field: "destination URL",- query: longestSearchToken(destinationUrl),- },];}diff --git a/apps/web/tests/partners/get-partners-count-search.test.ts b/apps/web/tests/partners/get-partners-count-search.test.tsindex e22e43dec6..1fa49e6e4b 100644--- a/apps/web/tests/partners/get-partners-count-search.test.ts+++ b/apps/web/tests/partners/get-partners-count-search.test.ts@@ -11,8 +11,6 @@ const mocks = vi.hoisted(() => ({partnerGroupBy: vi.fn(),partnerTagGroupBy: vi.fn(),applicationEventGroupBy: vi.fn(),- enrollmentFindUnique: vi.fn(),- linkFindUnique: vi.fn(),}));vi.mock("@/lib/prisma", () => ({@@ -21,12 +19,10 @@ vi.mock("@/lib/prisma", () => ({programEnrollment: {count: mocks.count,groupBy: mocks.enrollmentGroupBy,- findUnique: mocks.enrollmentFindUnique,},partner: { groupBy: mocks.partnerGroupBy, findUnique: vi.fn() },programPartnerTag: { groupBy: mocks.partnerTagGroupBy },programApplicationEvent: { groupBy: mocks.applicationEventGroupBy },- link: { findUnique: mocks.linkFindUnique },},}));@@ -77,29 +73,6 @@ describe("getPartnersCount search", () => {expect(mocks.count).not.toHaveBeenCalled();});- it("counts an exact short-link result from its hits, not the aggregation", async () => {- // The aggregation would count every document sharing a token with the- // pasted link, which is most of the program.- mocks.linkFindUnique.mockResolvedValue({- programId: "prog_test",- partnerId: "pn_1",- });- mocks.enrollmentFindUnique.mockResolvedValue({ id: "pge_9" });- mocks.count.mockResolvedValue(1);- const searchProvider = createSearchProvider(12_000);-- const count = await getPartnersCount<number>(- { programId: "prog_test", search: "go.acme.com/partnername" },- { searchProvider },- );-- expect(count).toBe(1);- expect(searchProvider.countCandidates).not.toHaveBeenCalled();- expect(mocks.count).toHaveBeenCalledWith({- where: expect.objectContaining({ id: { in: ["pge_9"] } }),- });- });-it.each([["a tenant ID", { tenantId: "tenant_1" }],["explicit partner IDs", { partnerIds: ["pn_1"] }],diff --git a/apps/web/tests/partners/partner-search-backfill.test.ts b/apps/web/tests/partners/partner-search-backfill.test.tsindex cc208f4dd9..62730b1294 100644--- a/apps/web/tests/partners/partner-search-backfill.test.ts+++ b/apps/web/tests/partners/partner-search-backfill.test.ts@@ -25,9 +25,6 @@ function createSource(id: string): PartnerSearchDocumentSource {partnerId: `pn_${id}`,status: "approved" as const,groupId: null,- program: {- url: "https://example.com",- },partner: {name: "Rafi Hasan",email: "[email protected]",diff --git a/apps/web/tests/partners/partner-search-document.test.ts b/apps/web/tests/partners/partner-search-document.test.tsindex f0c4002540..200a71c4d0 100644--- a/apps/web/tests/partners/partner-search-document.test.ts+++ b/apps/web/tests/partners/partner-search-document.test.ts@@ -10,9 +10,6 @@ const source: PartnerSearchDocumentSource = {partnerId: "pn_test",status: "approved" as const,groupId: "grp_test",- program: {- url: "https://www.example.com/landing",- },partner: {name: "Rafi Hasan",email: "[email protected]",@@ -35,16 +32,7 @@ const source: PartnerSearchDocumentSource = {},],},- links: [- {- key: "rafi",- url: "https://example.com/referrals/rafi?utm_source=partner&token=abc",- },- {- key: "rafi-tools",- url: "https://rafi.dev/tools#pricing",- },- ],+ links: [{ key: "rafi" }, { key: "rafi-tools" }],};describe("serializePartnerSearchDocument", () => {@@ -60,12 +48,6 @@ describe("serializePartnerSearchDocument", () => {platformTypes: ["website", "twitter"],platformIdentifiers: ["https://rafi.dev", "@rafi-on-x"],linkKeys: ["rafi", "rafi-tools"],- destinationUrls: [- // The program's own host is dropped, the query string with it. A- // partner-specific host is kept: it identifies the partner.- "/referrals/rafi",- "rafi.dev/tools",- ],status: "approved",groupId: "grp_test",country: "US",@@ -74,31 +56,4 @@ describe("serializePartnerSearchDocument", () => {});});- it("drops a destination that is only the program's root", () => {- const document = serializePartnerSearchDocument({- ...source,- links: [{ key: "root", url: "https://example.com/" }],- });-- expect(document.destinationUrls).toEqual([]);- });-- it("keeps the whole host when the program has no URL", () => {- const document = serializePartnerSearchDocument({- ...source,- program: { url: null },- links: [{ key: "rafi", url: "https://example.com/referrals/rafi" }],- });-- expect(document.destinationUrls).toEqual(["example.com/referrals/rafi"]);- });-- it("keeps an unparseable destination as is", () => {- const document = serializePartnerSearchDocument({- ...source,- links: [{ key: "raw", url: "not a url" }],- });-- expect(document.destinationUrls).toEqual(["not a url"]);- });});diff --git a/apps/web/tests/partners/partner-search-find-candidates.test.ts b/apps/web/tests/partners/partner-search-find-candidates.test.tsindex 0a24c236af..e06bc59b64 100644--- a/apps/web/tests/partners/partner-search-find-candidates.test.ts+++ b/apps/web/tests/partners/partner-search-find-candidates.test.ts@@ -2,18 +2,10 @@ import type { PartnerSearchProvider } from "@/lib/api/partners/search";import { findPartnerSearchCandidates } from "@/lib/api/partners/search";import { beforeEach, describe, expect, it, vi } from "vitest";-const mocks = vi.hoisted(() => ({- findUnique: vi.fn(),- linkFindUnique: vi.fn(),- enrollmentFindUnique: vi.fn(),-}));+const mocks = vi.hoisted(() => ({ findUnique: vi.fn() }));vi.mock("@/lib/prisma", () => ({- prisma: {- partner: { findUnique: mocks.findUnique },- link: { findUnique: mocks.linkFindUnique },- programEnrollment: { findUnique: mocks.enrollmentFindUnique },- },+ prisma: { partner: { findUnique: mocks.findUnique } },}));function createProvider(@@ -38,75 +30,9 @@ const query = (search: string) => ({describe("findPartnerSearchCandidates", () => {beforeEach(() => {mocks.findUnique.mockReset();- mocks.linkFindUnique.mockReset();- mocks.enrollmentFindUnique.mockReset();vi.spyOn(console, "error").mockImplementation(() => {});});- describe("pasted short links", () => {- it.each([- ["with protocol", "https://go.acme.com/partnername"],- ["without protocol", "go.acme.com/partnername"],- ])("resolves %s to exactly that enrollment", async (_label, search) => {- mocks.linkFindUnique.mockResolvedValue({- programId: "prog_1",- partnerId: "pn_1",- });- mocks.enrollmentFindUnique.mockResolvedValue({ id: "pge_9" });- const provider = createProvider();-- await expect(- findPartnerSearchCandidates(provider, query(search)),- ).resolves.toEqual({ hits: [{ id: "pge_9" }], exact: true });-- expect(mocks.linkFindUnique).toHaveBeenCalledWith({- where: { shortLink: "https://go.acme.com/partnername" },- select: { programId: true, partnerId: true },- });- expect(provider.searchCandidates).not.toHaveBeenCalled();- });-- it("returns no hits for a link from another program", async () => {- // Its domain tokens would only flood the ranked results.- mocks.linkFindUnique.mockResolvedValue({- programId: "prog_other",- partnerId: "pn_1",- });- const provider = createProvider();-- await expect(- findPartnerSearchCandidates(provider, query("go.acme.com/theirs")),- ).resolves.toEqual({ hits: [], exact: true });-- expect(provider.searchCandidates).not.toHaveBeenCalled();- });-- it("falls through to the ranked search on a miss", async () => {- // A partial paste still matches the link key by prefix.- mocks.linkFindUnique.mockResolvedValue(null);- const provider = createProvider();-- await expect(- findPartnerSearchCandidates(provider, query("go.acme.com/partnern")),- ).resolves.toEqual({ hits: [{ id: "pge_1" }] });-- expect(provider.searchCandidates).toHaveBeenCalledOnce();- });-- it.each([- ["a bare domain", "go.acme.com"],- ["a name", "drew moore"],- ["a key alone", "partnername"],- ])("does not treat %s as a short link", async (_label, search) => {- const provider = createProvider();-- await findPartnerSearchCandidates(provider, query(search));-- expect(mocks.linkFindUnique).not.toHaveBeenCalled();- expect(provider.searchCandidates).toHaveBeenCalledOnce();- });- });-it("answers a known email from the database, without calling the provider", async () => {mocks.findUnique.mockResolvedValue({ id: "pn_1" });const provider = createProvider();diff --git a/apps/web/tests/partners/partner-search-sync.test.ts b/apps/web/tests/partners/partner-search-sync.test.tsindex 195418fd07..6973a2e49f 100644--- a/apps/web/tests/partners/partner-search-sync.test.ts+++ b/apps/web/tests/partners/partner-search-sync.test.ts@@ -28,9 +28,6 @@ function createSource(partnerId: `pn_${id}`,status: "approved" as const,groupId: null,- program: {- url: "https://example.com",- },partner: {name: "Rafi Hasan",email: "[email protected]",diff --git a/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts b/apps/web/tests/partners/turbopuffer-partner-search-provider.test.tsindex 1fe6cb043c..e3a9409c76 100644--- a/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts+++ b/apps/web/tests/partners/turbopuffer-partner-search-provider.test.ts@@ -21,7 +21,6 @@ const document: PartnerSearchDocument = {platformTypes: ["youtube"],platformIdentifiers: ["@rafi"],linkKeys: ["rafi-link"],- destinationUrls: ["/referrals/rafi"],status: "approved",groupId: "grp_test",country: "US",@@ -122,7 +121,6 @@ describe("Turbopuffer partner search provider", () => {"youtube","@rafi","rafi-link",- "/referrals/rafi",]) {expect(row.searchText).toContain(value);}