The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
DeepLinkPage
Steven Tey1c3aa7a6c2e2
diff --git a/apps/web/app/deeplink/[url]/page.tsx b/apps/web/app/custom-uri-scheme/[url]/page.tsxsimilarity index 93%rename from apps/web/app/deeplink/[url]/page.tsxrename to apps/web/app/custom-uri-scheme/[url]/page.tsxindex e0568e8a88..2799015532 100644--- a/apps/web/app/deeplink/[url]/page.tsx+++ b/apps/web/app/custom-uri-scheme/[url]/page.tsx@@ -1,6 +1,10 @@export const runtime = "edge";-export default function DeepLinkPage({ params }: { params: { url: string } }) {+export default function CustomURISchemePage({+ params,+}: {+ params: { url: string };+}) {// First decode the full URL parameter from the routeconst url = decodeURIComponent(params.url);// Split into base URL and query stringdiff --git a/apps/web/app/deeplink/[domain]/[key]/action-buttons.tsx b/apps/web/app/deeplink/[domain]/[key]/action-buttons.tsxnew file mode 100644index 0000000000..ea2b0f6fae--- /dev/null+++ b/apps/web/app/deeplink/[domain]/[key]/action-buttons.tsx@@ -0,0 +1,52 @@+"use client";++import { EdgeLinkProps } from "@/lib/planetscale";+import { Button, Check2, Copy, Wordmark } from "@dub/ui";+import Link from "next/link";+import { useState } from "react";++export function DeepLinkActionButtons({ link }: { link: EdgeLinkProps }) {+ const [copied, setCopied] = useState(false);++ const handleClick = async ({ withCopy }: { withCopy?: boolean } = {}) => {+ if (withCopy) {+ try {+ await navigator.clipboard.writeText(link.shortLink);+ setCopied(true);+ setTimeout(() => setCopied(false), 2000);+ } catch (err) {+ console.error("Failed to copy:", err);+ }+ }+ window.location.href = link.ios || link.url;+ };++ return (+ <div className="flex flex-col items-center gap-4 pt-8">+ <Link+ href="https://dub.co"+ target="_blank"+ className="flex items-center gap-1.5 whitespace-nowrap text-sm"+ >+ Powered by <Wordmark className="h-4 p-0.5" />+ </Link>++ <Button+ text="Get the App"+ className="h-11 w-full rounded-xl"+ variant="primary"+ onClick={() => handleClick({ withCopy: true })}+ icon={+ copied ? <Check2 className="size-4" /> : <Copy className="size-4" />+ }+ />++ <button+ onClick={() => handleClick()}+ className="text-sm text-neutral-400"+ >+ Get the app without copying+ </button>+ </div>+ );+}diff --git a/apps/web/app/deeplink/[domain]/[key]/page.tsx b/apps/web/app/deeplink/[domain]/[key]/page.tsxnew file mode 100644index 0000000000..dbedad04af--- /dev/null+++ b/apps/web/app/deeplink/[domain]/[key]/page.tsx@@ -0,0 +1,47 @@+import { getLinkViaEdge } from "@/lib/planetscale";+import { BlurImage } from "@dub/ui";+import { DUB_LOGO } from "@dub/utils";+import { notFound } from "next/navigation";+import { DeepLinkActionButtons } from "./action-buttons";++export const runtime = "edge";++export default async function DeepLinkPage({+ params,+}: {+ params: { domain: string; key: string };+}) {+ const domain = params.domain;+ const key = decodeURIComponent(params.key);++ // Get link data for displaying app info+ const link = await getLinkViaEdge({ domain, key });+ console.log({ link });++ if (!link) {+ notFound();+ }++ return (+ <main className="flex h-dvh w-full flex-col justify-between bg-gray-50 px-4 py-8">+ {/* Top section with logo and title */}+ <div className="flex flex-col items-center gap-y-6">+ <div className="rounded-lg border border-neutral-200 p-2 shadow-md">+ <BlurImage+ src={DUB_LOGO}+ alt="Dub Logo"+ width={48}+ height={48}+ className="size-12"+ />+ </div>++ <h2 className="font-display text-xl font-bold text-gray-900">+ Open this page in the app+ </h2>+ </div>++ <DeepLinkActionButtons link={link} />+ </main>+ );+}diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.tsindex 073f4970ef..7da022c11a 100644--- a/apps/web/lib/middleware/link.ts+++ b/apps/web/lib/middleware/link.ts@@ -2,7 +2,7 @@ import {createResponseWithCookies,detectBot,getFinalUrl,- isSupportedDeeplinkProtocol,+ isSupportedCustomURIScheme,parse,} from "@/lib/middleware/utils";import { recordClick } from "@/lib/tinybird";@@ -316,7 +316,7 @@ export default async function LinkMiddleware();// rewrite to deeplink page if the link is a mailto: or tel:- } else if (isSupportedDeeplinkProtocol(url)) {+ } else if (isSupportedCustomURIScheme(url)) {ev.waitUntil(recordClick({req,@@ -334,7 +334,7 @@ export default async function LinkMiddleware(return createResponseWithCookies(NextResponse.rewrite(new URL(- `/deeplink/${encodeURIComponent(+ `/custom-uri-scheme/${encodeURIComponent(getFinalUrl(url, {req,...(shouldCacheClickId && { clickId }),@@ -396,33 +396,47 @@ export default async function LinkMiddleware(// redirect to iOS link if it is specified and the user is on an iOS device} else if (ios && ua.os?.name === "iOS") {ev.waitUntil(- Promise.all([- recordClick({+ recordClick({+ req,+ clickId,+ linkId,+ domain,+ key,+ url: ios,+ webhookIds,+ workspaceId,+ shouldCacheClickId,+ }),+ );++ // if it's an iOS app store URL, we need to show the interstitial page ++ if (ios.startsWith("https://apps.apple.com/")) {+ ev.waitUntil(+ cacheDeepLinkClickData({req,clickId,- linkId,- domain,- key,- url: ios,- webhookIds,- workspaceId,- shouldCacheClickId,+ link: {+ id: linkId,+ domain,+ key,+ url, // pass the main destination URL to the cache (for deferred deep linking)+ },}),- // cache click if it's an iOS app store URL- ios.startsWith("https://apps.apple.com/") &&- cacheDeepLinkClickData({- req,- clickId,- link: {- id: linkId,- domain,- key,- url, // pass the main destination URL to the cache (for deferred deep linking)+ );+ // rewrite to the deeplink interstitial page+ return createResponseWithCookies(+ NextResponse.rewrite(+ new URL(`/deeplink/${domain}/${encodeURIComponent(key)}`, req.url),+ {+ headers: {+ ...DUB_HEADERS,+ ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),},- }),- ]),- );-+ },+ ),+ cookieData,+ );+ }return createResponseWithCookies(NextResponse.redirect(getFinalUrl(ios, {diff --git a/apps/web/lib/middleware/utils/index.ts b/apps/web/lib/middleware/utils/index.tsindex d9455c4edf..ea11bdd477 100644--- a/apps/web/lib/middleware/utils/index.ts+++ b/apps/web/lib/middleware/utils/index.ts@@ -3,6 +3,6 @@ export * from "./detect-bot";export * from "./detect-qr";export * from "./get-final-url";export * from "./get-identity-hash";-export * from "./is-supported-deeplink-protocol";+export * from "./is-supported-custom-uri-scheme";export * from "./is-valid-internal-redirect";export * from "./parse";diff --git a/apps/web/lib/middleware/utils/is-supported-custom-uri-scheme.ts b/apps/web/lib/middleware/utils/is-supported-custom-uri-scheme.tsnew file mode 100644index 0000000000..f7a274e637--- /dev/null+++ b/apps/web/lib/middleware/utils/is-supported-custom-uri-scheme.ts@@ -0,0 +1,12 @@+const supportedCustomURISchemes = [+ "mailto:",+ "sms:",+ "tel:",+ "tg:",+ "whatsapp:",+ "xmpp:",+];++export const isSupportedCustomURIScheme = (url: string) => {+ return supportedCustomURISchemes.some((protocol) => url.startsWith(protocol));+};diff --git a/apps/web/lib/middleware/utils/is-supported-deeplink-protocol.ts b/apps/web/lib/middleware/utils/is-supported-deeplink-protocol.tsdeleted file mode 100644index 3ebcd9320e..0000000000--- a/apps/web/lib/middleware/utils/is-supported-deeplink-protocol.ts+++ /dev/null@@ -1,14 +0,0 @@-const supportedDeeplinkProtocols = [- "mailto:",- "sms:",- "tel:",- "tg:",- "whatsapp:",- "xmpp:",-];--export const isSupportedDeeplinkProtocol = (url: string) => {- return supportedDeeplinkProtocols.some((protocol) =>- url.startsWith(protocol),- );-};diff --git a/apps/web/lib/planetscale/types.ts b/apps/web/lib/planetscale/types.tsindex c97644e0e3..3c1fff5329 100644--- a/apps/web/lib/planetscale/types.ts+++ b/apps/web/lib/planetscale/types.ts@@ -3,6 +3,7 @@ export interface EdgeLinkProps {domain: string;key: string;url: string;+ shortLink: string;proxy: number;title: string;description: string;diff --git a/packages/utils/src/constants/main.ts b/packages/utils/src/constants/main.tsindex 7cfa86159d..7436b9a267 100644--- a/packages/utils/src/constants/main.ts+++ b/packages/utils/src/constants/main.ts@@ -64,6 +64,7 @@ export const PARTNERS_DOMAIN_WITH_NGROK =: process.env.NEXT_PUBLIC_NGROK_URL || "http://partners.localhost:8888";export const DUB_LOGO = "https://assets.dub.co/logo.png";+export const DUB_LOGO_SQUARE = "https://assets.dub.co/logo-square.png";export const DUB_QR_LOGO = "https://assets.dub.co/logo.png";export const DUB_WORDMARK = "https://assets.dub.co/wordmark.png";export const DUB_THUMBNAIL = "https://assets.dub.co/thumbnail.jpg";