The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update notfoundlink implementation
Steven Teycd4fe6377f8b
diff --git a/apps/web/app/not-found/[domain]/page.tsx b/apps/web/app/notfoundlink/page.tsxsimilarity index 79%rename from apps/web/app/not-found/[domain]/page.tsxrename to apps/web/app/notfoundlink/page.tsxindex bb8f91e5ca..91e3b95d94 100644--- a/apps/web/app/not-found/[domain]/page.tsx+++ b/apps/web/app/notfoundlink/page.tsx@@ -1,7 +1,5 @@-import { getDomainViaEdge } from "@/lib/planetscale/get-domain-via-edge";import { Background, Footer, Nav, NavMobile } from "@dub/ui";import { constructMetadata } from "@dub/utils";-import { redirect } from "next/navigation";export const runtime = "edge";@@ -12,17 +10,7 @@ export const metadata = constructMetadata({noIndex: true,});-export default async function NotFoundLinkPage({- params,-}: {- params: { domain: string };-}) {- const domain = await getDomainViaEdge(params.domain);-- if (domain?.notFoundUrl) {- redirect(domain.notFoundUrl);- }-+export default async function NotFoundLinkPage() {return (<main className="flex min-h-screen flex-col justify-between"><NavMobile />diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.tsindex 9ab4d7d99b..9ef71fc255 100644--- a/apps/web/lib/middleware/link.ts+++ b/apps/web/lib/middleware/link.ts@@ -23,6 +23,7 @@ import {userAgent,} from "next/server";import { getLinkViaEdge } from "../planetscale";+import { getDomainViaEdge } from "../planetscale/get-domain-via-edge";import { RedisLinkProps } from "../types";export default async function LinkMiddleware(@@ -56,11 +57,20 @@ export default async function LinkMiddleware(const linkData = await getLinkViaEdge(domain, key);if (!linkData) {- // short link not found, rewrite to not-found page- // TODO: log 404s (https://github.com/dubinc/dub/issues/559)- return NextResponse.rewrite(new URL(`/not-found/${domain}`, req.url), {- headers: DUB_HEADERS,- });+ // check if domain has notFoundUrl configured+ const domainData = await getDomainViaEdge(domain);+ if (domainData?.notFoundUrl) {+ return NextResponse.redirect(domainData.notFoundUrl, {+ headers: {+ ...DUB_HEADERS,+ "X-Robots-Tag": "googlebot: noindex",+ },+ });+ } else {+ return NextResponse.rewrite(new URL("/notfoundlink", req.url), {+ headers: DUB_HEADERS,+ });+ }}// format link to fit the RedisLinkProps interfacediff --git a/apps/web/scripts/update-not-found.ts b/apps/web/scripts/update-not-found.tsnew file mode 100644index 0000000000..63006b1b4d--- /dev/null+++ b/apps/web/scripts/update-not-found.ts@@ -0,0 +1,71 @@+import { prisma } from "@/lib/prisma";+import "dotenv-flow/config";++async function main() {+ const domains = await prisma.domain.findMany({+ where: {+ notFoundUrl: null,+ project: {+ plan: {+ not: "free",+ },+ },+ links: {+ some: {+ key: "_root",+ url: {+ not: "",+ },+ },+ },+ },+ select: {+ id: true,+ slug: true,+ notFoundUrl: true,+ project: {+ select: {+ slug: true,+ plan: true,+ },+ },+ links: {+ select: {+ key: true,+ url: true,+ },+ where: {+ key: "_root",+ },+ },+ },+ take: 10,+ orderBy: {+ createdAt: "asc",+ },+ });++ const updatedDomains = await Promise.all(+ domains.map((domain) => {+ return prisma.domain.update({+ where: { id: domain.id },+ data: { notFoundUrl: domain.links[0].url },+ select: {+ id: true,+ slug: true,+ notFoundUrl: true,+ project: {+ select: {+ slug: true,+ plan: true,+ },+ },+ },+ });+ }),+ );++ console.table(updatedDomains);+}++main();