The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add Vercel-CDN-Cache for not found links (#3626)
Steven Teyc91872f0f67d
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.tsindex e2810134a4..64eeb38185 100644--- a/apps/web/app/api/domains/[domain]/route.ts+++ b/apps/web/app/api/domains/[domain]/route.ts@@ -16,6 +16,7 @@ import { prisma } from "@dub/prisma";import { Prisma } from "@dub/prisma/client";import { combineWords, nanoid, R2_URL } from "@dub/utils";import { waitUntil } from "@vercel/functions";+import { revalidateTag } from "next/cache";import { NextResponse } from "next/server";import * as z from "zod/v4";@@ -48,6 +49,8 @@ export const PATCH = withWorkspace(slug: domain,registeredDomain,logo: oldLogo,+ notFoundUrl: oldNotFoundUrl,+ expiredUrl: oldExpiredUrl,partnerProgram,} = await getDomainOrThrow({workspace,@@ -200,6 +203,13 @@ export const PATCH = withWorkspace(await storage.delete({ key: oldLogo.replace(`${R2_URL}/`, "") });}+ if (+ (notFoundUrl !== undefined && notFoundUrl !== oldNotFoundUrl) ||+ (expiredUrl !== undefined && expiredUrl !== oldExpiredUrl)+ ) {+ revalidateTag(`notfound:${domain}`);+ }+if (domainUpdated) {await Promise.all([// remove old domain from Verceldiff --git a/apps/web/app/api/supported-countries/route.ts b/apps/web/app/api/supported-countries/route.tsindex 423465c578..eed91ca2af 100644--- a/apps/web/app/api/supported-countries/route.ts+++ b/apps/web/app/api/supported-countries/route.ts@@ -8,7 +8,8 @@ export async function GET() {headers: {"Access-Control-Allow-Origin": "*",// cache indefinitely till next deployment- "Cache-Control": "public, max-age=31536000, s-maxage=31536000",+ "Vercel-CDN-Cache-Control": "s-maxage=31536000",+ "Cache-Control": "public, max-age=31536000",},});}diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.tsindex dfc93201fb..9ecce2b06e 100644--- a/apps/web/lib/api/links/cache.ts+++ b/apps/web/lib/api/links/cache.ts@@ -7,6 +7,7 @@ import {} from "@/lib/upstash";import { getCache, waitUntil } from "@vercel/functions";import { LRUCache } from "lru-cache";+import { revalidateTag } from "next/cache";import { decodeKey, isCaseSensitiveDomain } from "./case-sensitivity";import { ExpandedLink } from "./utils/transform-link";@@ -40,6 +41,7 @@ class LinkCache {const redisLink = formatRedisLink(link);const cacheKey = this._createKey({ domain: link.domain, key: link.key });pipeline.set(cacheKey, redisLink, { ex: REDIS_CACHE_EXPIRATION });+ revalidateTag(`notfound:${link.domain}:${link.key}`);});return await pipeline.exec();@@ -51,12 +53,13 @@ class LinkCache {// Update LRU cache immediately to prevent stale readslinkLRUCache.set(cacheKey, redisLink);+ revalidateTag(`notfound:${link.domain}:${link.key}`);await Promise.all([redisGlobal.set(cacheKey, redisLink, {ex: REDIS_CACHE_EXPIRATION,}),- this._invalidateVercelCache(cacheKey),+ this._invalidateVercelRuntimeCache(cacheKey),]);}@@ -126,7 +129,7 @@ class LinkCache {async delete({ domain, key }: Pick<LinkProps, "domain" | "key">) {const cacheKey = this._createKey({ domain, key });- waitUntil(this._invalidateVercelCache(cacheKey));+ waitUntil(this._invalidateVercelRuntimeCache(cacheKey));return await redisGlobal.del(cacheKey);}@@ -167,9 +170,16 @@ class LinkCache {return caseSensitive ? cacheKey : cacheKey.toLowerCase();}+ _createNotFoundCacheKeys({ domain, key }: Pick<LinkProps, "domain" | "key">) {+ // here we set 2 cache tags to invalidate the cache:+ // 1. notfound:${domain}:${key} - for the specific not found link+ // 2. notfound:${domain} - scope all links under the domain (for easy purging in PATCH /domains/:domain)+ return `notfound:${domain}:${key},notfound:${domain}`;+ }+// Vercel cache reads are 10x cheaper than writes, so to invalidate the cache// we check if the value is cached first before deleting it.- async _invalidateVercelCache(cacheKey: string) {+ private async _invalidateVercelRuntimeCache(cacheKey: string) {return vercelCache.get(cacheKey).then((cachedLink) =>diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.tsindex b9c277c2f6..91c0b3bae1 100644--- a/apps/web/lib/middleware/link.ts+++ b/apps/web/lib/middleware/link.ts@@ -273,16 +273,24 @@ export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {}),);- return createResponseWithCookies(- NextResponse.rewrite(new URL(`/${domain}`, req.url), {+ const rewriteResponse = NextResponse.rewrite(+ new URL(`/${domain}`, req.url),+ {headers: {...DUB_HEADERS,- // we only index root domain links if they're not subdomains- ...(shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},- }),- cookieData,+ },+ );+ rewriteResponse.headers.set(+ "Vercel-CDN-Cache-Control",+ "public, s-maxage=86400",+ );+ rewriteResponse.headers.set(+ "Vercel-Cache-Tag",+ linkCache._createNotFoundCacheKeys({ domain, key: "_root" }), // set cache tag for root domain link);+ return createResponseWithCookies(rewriteResponse, cookieData);}const isBot = detectBot(req);diff --git a/apps/web/lib/middleware/utils/handle-not-found-link.ts b/apps/web/lib/middleware/utils/handle-not-found-link.tsindex b43639df61..d904f66eb9 100644--- a/apps/web/lib/middleware/utils/handle-not-found-link.ts+++ b/apps/web/lib/middleware/utils/handle-not-found-link.ts@@ -1,15 +1,17 @@+import { linkCache } from "@/lib/api/links/cache";import { getDomainViaEdge } from "@/lib/planetscale/get-domain-via-edge";import { DUB_HEADERS } from "@dub/utils";import { NextRequest, NextResponse } from "next/server";import { parse } from "./parse";export const handleNotFoundLink = async (req: NextRequest) => {- const { domain } = parse(req);+ const { domain, fullKey } = parse(req);+ let response: NextResponse;// check if domain has notFoundUrl configuredconst domainData = await getDomainViaEdge(domain);if (domainData?.notFoundUrl) {- return NextResponse.redirect(domainData.notFoundUrl, {+ response = NextResponse.redirect(domainData.notFoundUrl, {headers: {...DUB_HEADERS,"X-Robots-Tag": "googlebot: noindex",@@ -19,8 +21,14 @@ export const handleNotFoundLink = async (req: NextRequest) => {status: 302,});} else {- return NextResponse.rewrite(new URL(`/${domain}/not-found`, req.url), {+ response = NextResponse.rewrite(new URL(`/${domain}/not-found`, req.url), {headers: DUB_HEADERS,});}+ response.headers.set("Vercel-CDN-Cache-Control", "public, s-maxage=86400");+ response.headers.set(+ "Vercel-Cache-Tag",+ linkCache._createNotFoundCacheKeys({ domain, key: fullKey }),+ );+ return response;};diff --git a/apps/web/lib/planetscale/get-domain-via-edge.ts b/apps/web/lib/planetscale/get-domain-via-edge.tsindex a5d38aace5..fd1390bd09 100644--- a/apps/web/lib/planetscale/get-domain-via-edge.ts+++ b/apps/web/lib/planetscale/get-domain-via-edge.ts@@ -1,12 +1,28 @@+import { LRUCache } from "lru-cache";import { conn } from "./connection";import { EdgeDomainProps } from "./types";+const domainLRUCache = new LRUCache<string, EdgeDomainProps>({+ max: 1000,+ ttl: 5 * 60 * 1000, // 5 minutes+});+export const getDomainViaEdge = async (domain: string) => {+ const cached = domainLRUCache.get(domain);+ if (cached) {+ return cached;+ }+const { rows } =(await conn.execute<EdgeDomainProps>("SELECT * FROM Domain WHERE slug = ?",[domain],)) || {};- return rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;+ const result =+ rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;+ if (result !== null) {+ domainLRUCache.set(domain, result);+ }+ return result;};