The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
singleflight fallback (#3568)
Pedro Ladeira5fa81bfd6b5a
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.tsindex c5a54cdf73..dfc93201fb 100644--- a/apps/web/lib/api/links/cache.ts+++ b/apps/web/lib/api/links/cache.ts@@ -107,8 +107,8 @@ class LinkCache {domain,key,});- // no link found, throw a 404 errorif (!linkData) {+ // if no link found (and Redis fails), throw a 404 error (don't rewrite to /not-found since it's expensive)throw new Error("Link not found.");}// else, format the link and cache it both in LRU cache and Vercel cachediff --git a/apps/web/lib/planetscale/get-link-via-edge.ts b/apps/web/lib/planetscale/get-link-via-edge.tsindex 015f904ad0..9ec1bcdff9 100644--- a/apps/web/lib/planetscale/get-link-via-edge.ts+++ b/apps/web/lib/planetscale/get-link-via-edge.ts@@ -7,7 +7,7 @@ import {import { conn } from "./connection";import { EdgeLinkProps, EdgeLinkWithWebhooks } from "./types";-export const getLinkViaEdge = async ({+const getLinkViaEdgeHelper = async ({domain,key,}: {@@ -46,3 +46,32 @@ export const getLinkViaEdge = async ({webhooks,};};++const inFlightLinkLookups = new Map<+ string,+ Promise<Awaited<ReturnType<typeof getLinkViaEdgeHelper>>>+>();++export const getLinkViaEdge = async ({+ domain,+ key,+}: {+ domain: string;+ key: string;+}): Promise<Awaited<ReturnType<typeof getLinkViaEdgeHelper>>> => {+ const lookupKey = `${domain}:${key}`;+ const existingLookup = inFlightLinkLookups.get(lookupKey);++ if (existingLookup) {+ console.log(`[getLinkViaEdge] ${lookupKey} - Existing lookup found`);+ return await existingLookup;+ }++ const lookupPromise = getLinkViaEdgeHelper({ domain, key }).finally(() => {+ inFlightLinkLookups.delete(lookupKey);+ });++ inFlightLinkLookups.set(lookupKey, lookupPromise);++ return await lookupPromise;+};