I build iBuat.Link, a link manager for small businesses that sell through WhatsApp, Instagram and marketplaces. The feature people care about most sounds boring: change where a short link or a printed QR code goes, after it's out in the world. A seller prints 2,000 boxes with a QR code, the marketplace listing behind it gets taken down, and the code has to open something else by tomorrow.
Doing that well comes down to three small decisions in the redirect path. None of them are clever, but each one was a bug before it was a decision.
1. Never send a 301
The first instinct for a URL shortener is 301 Moved Permanently: it's the "SEO-friendly" redirect, and browsers love it. That's exactly the problem. Browsers cache a 301 aggressively, sometimes for as long as the user keeps their profile. Change the destination in the dashboard and anyone who clicked before keeps landing on the old page, and you can't reach their cache to fix it.
So every redirect is a 302, and it says out loud that it must not be cached:
// Links are editable. A permanent redirect would let browsers cache an // outdated destination after the owner changes it. return NextResponse.redirect(destination, { status: 302, headers: { 'Cache-Control': 'private, no-store' }, }); private, no-store also keeps CDNs and corporate proxies from holding on to it. The cost is one extra request to us on every click, which is the whole point: the decision about where to send someone is made fresh each time.
2. Cache the lookup for two seconds, not the answer
If every click hits the database, a link that goes viral turns into a primary-key lookup storm. If you cache for minutes, edits feel broken ("I changed it, why does it still open the old page?").
The compromise is a tiny read-through cache in front of the lookup, with a TTL of two seconds:
const TTL_MS = 2_000; const cache = new Map<string, { link: PublicLink | null; expires: number }>(); export async function resolvePublicLink(slug: string) { const hit = cache.get(slug); if (hit && hit.expires > Date.now()) return hit.link; const link = await loadLink(slug); // one indexed query cache.set(slug, { link, expires: Date.now() + TTL_MS }); return link; } /** Called by the dashboard's update route, so the editing process sees it instantly. */ export function invalidatePublicLink(slug: string) { cache.delete(slug); } A burst of a thousand clicks in a second costs one query. An edit is visible within two seconds everywhere, and immediately in the process that handled it. Nobody has ever noticed two seconds, and the database graph stopped spiking.
Note what's cached: the link row, not the redirect. Everything that decides the answer is evaluated on each request against that row: an expiry date is compared with the clock every time, so an expired link stops exactly on time, and a link with a click limit reads a fresh count. Switching a link off follows the same two-second rule as an edit.
3. Facebook's crawler is not your customer
The first time a link ran in a Facebook ad, the click count looked fantastic. Then we split it by user agent. A large share of the "clicks" were facebookexternalhit, the bot Facebook sends to build a link preview, plus WhatsApp's own preview fetcher (WhatsApp/2.x), plus the usual crawlers.
Those requests still get redirected (a preview with no title looks broken), but they're counted separately and never use up the owner's monthly click quota:
const BOT_USER_AGENT = /(?:(?<!cu)bot|crawler|spider|curl|wget|python|headless|facebookexternalhit|^whatsapp\/)/i; const isBot = BOT_USER_AGENT.test(userAgent); if (!isBot) await consumeTrackedClick(ownerId); // quota is for people recordVisit({ linkId, isBot }); // stored, hidden by default Two details that took a while to get right:
-
(?<!cu)bot: without the lookbehind,cubot(a phone brand that shows up in Android user agents) was classified as a bot, and real people on those phones vanished from the stats. -
^whatsapp\/is anchored. WhatsApp's link-preview fetcher sends exactly that prefix; WhatsApp's in-app browser sends a normal mobile user agent, and those are real visits.
The rule underneath: billing never decides whether someone arrives
The last lesson is about ordering. Quota checks, analytics writes and fraud scoring all happen after the destination is known, and none of them can stop the redirect. If the analytics write fails because the database is slow during a traffic spike, the error is logged and the visitor still lands where they were going.
try { const allowed = isBot ? await hasRoom(ownerId) : await consumeTrackedClick(ownerId); if (allowed) queueAnalytics(event); } catch (error) { console.error('[REDIRECT_ANALYTICS]', error); // never surfaces to the visitor } return redirect302(destination); A visitor scanning a QR code on a box doesn't know or care that the seller is out of quota this month. The link has to work.
That's the whole trick behind editable links: no permanent redirects, a cache short enough that nobody notices it, and honest numbers about who actually clicked. If you're curious how it looks from the user's side, the dynamic vs static QR codes post explains it without any code.
