Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

16.6 KB
Raw Blame
import { recordClick as recordClickJob } from "@/lib/tinybird";
import { formatRedisLink } from "@/lib/upstash";
import {
  APP_DOMAIN,
  DUB_HEADERS,
  LEGAL_WORKSPACE_ID,
  LOCALHOST_GEO_DATA,
  REDIRECTION_QUERY_PARAM,
  isDubDomain,
  isGoogleClickTrackerDomain,
  isUnsupportedKey,
  isValidUrl,
  nanoid,
  punyEncode,
} from "@dub/utils";
import { geolocation } from "@vercel/functions";
import { cookies } from "next/headers";
import {
  NextFetchEvent,
  NextRequest,
  NextResponse,
  userAgent,
} from "next/server";
import { linkCache } from "../api/links/cache";
import { isCaseSensitiveDomain } from "../api/links/case-sensitivity";
import { recordClickCache } from "../api/links/record-click-cache";
import { getLinkViaEdge } from "../planetscale";
import { cacheDeepLinkClickData } from "./utils/cache-deeplink-click-data";
import { crawlBitly } from "./utils/crawl-bitly";
import { createResponseWithCookies } from "./utils/create-response-with-cookies";
import { detectBot } from "./utils/detect-bot";
import { getFinalUrl } from "./utils/get-final-url";
import { getIdentityHash } from "./utils/get-identity-hash";
import { isAppsFlyerTrackingUrl } from "./utils/is-appsflyer-tracking-url";
import { isGooglePlayStoreUrl } from "./utils/is-google-play-store-url";
import { isIosAppStoreUrl } from "./utils/is-ios-app-store-url";
import { isSingularTrackingUrl } from "./utils/is-singular-tracking-url";
import { isSupportedCustomURIScheme } from "./utils/is-supported-custom-uri-scheme";
import { parse } from "./utils/parse";
import { resolveABTestURL } from "./utils/resolve-ab-test-url";

export async function LinkMiddleware(req: NextRequest, ev: NextFetchEvent) {
  let { domain, fullKey: originalKey, fullPath, searchParamsObj } = parse(req);

  if (!domain) {
    return NextResponse.next();
  }

  // encode the key to ascii
  // links on Dub are case insensitive by default
  let key = punyEncode(originalKey);

  if (!isCaseSensitiveDomain(domain)) {
    key = key.toLowerCase();
  }

  const inspectMode = key.endsWith("+");
  // if inspect mode is enabled, remove the trailing `+` from the key
  if (inspectMode) {
    key = key.slice(0, -1);
  }

  // if key is empty string, set to _root (root domain link)
  if (key === "") {
    key = "_root";
  }

  const STATIC_PAGES_CACHE_HEADERS = {
    "Vercel-CDN-Cache-Control": "public, s-maxage=86400",
    "Vercel-Cache-Tag": linkCache._createStaticPagesCacheKeys({
      domain,
      key: originalKey || "_root",
    }),
  };

  // we don't support .php links (too much bot traffic)
  // hence we redirect to the root domain and add `dub-no-track` header to avoid tracking bot traffic
  if (isUnsupportedKey(key)) {
    return NextResponse.redirect(new URL("/?dub-no-track=1", req.url), {
      headers: {
        ...DUB_HEADERS,
        "X-Robots-Tag": "googlebot: noindex",
      },
      status: 302,
    });
  }

  let { cachedLink, redisFailOver } = await linkCache.get({ domain, key });

  // skip click tracking during Redis failover to avoid timeouts
  const recordClick = redisFailOver
    ? async () => {
        console.log("Redis failover detected, skipping click tracking...");
        return;
      }
    : recordClickJob;

  if (!cachedLink) {
    let linkData = await getLinkViaEdge({
      domain,
      key,
    });

    if (!linkData) {
      if (domain === "buff.ly") {
        return await crawlBitly(req, ev);
      }

      return NextResponse.rewrite(new URL(`/${domain}/notfound`, req.url), {
        headers: {
          ...DUB_HEADERS,
          ...STATIC_PAGES_CACHE_HEADERS,
        },
      });
    }

    // format link to fit the RedisLinkProps interface
    cachedLink = formatRedisLink(linkData as any);

    ev.waitUntil(linkCache.set(linkData as any, { skipRevalidateTag: true }));
  }

  const {
    id: linkId,
    password,
    trackConversion,
    geo: geoTargeting,
    proxy,
    rewrite,
    expiresAt,
    disabledAt,
    ios,
    android,
    expiredUrl,
    doIndex,
    testVariants,
    testCompletedAt,
    projectId: workspaceId,
  } = cachedLink;

  const testUrl = await resolveABTestURL({
    testVariants,
    testCompletedAt,
  });

  const url = testUrl || cachedLink.url;

  // if the following is true, we need to cache the clickId data (so it's available for subsequent /track/lead requests):
  // - trackConversion is enabled
  // - it's a Singular or AppsFlyer tracking URL
  const shouldCacheClickId =
    trackConversion ||
    isSingularTrackingUrl(url) ||
    isAppsFlyerTrackingUrl(url);

  // by default, we only index default dub domain links (e.g. dub.sh)
  // everything else is not indexed by default, unless the user has explicitly set it to be indexed
  const shouldIndex = isDubDomain(domain) || doIndex === true;

  const dubIdCookieName = `dub_id_${domain}_${key}`;

  let clickId: string | undefined;
  // only lookup/mint a new clickId if not in Redis failover mode
  if (!redisFailOver) {
    const cookieStore = await cookies();
    clickId = cookieStore.get(dubIdCookieName)?.value;
    if (!clickId) {
      // if we need to cache the clickId, check if clickId is cached in Redis
      if (shouldCacheClickId) {
        const identityHash = await getIdentityHash(req);
        clickId =
          (await recordClickCache
            .get({ domain, key, identityHash })
            .catch(() => undefined)) || undefined;
      }

      // if there's still no clickId, generate a new one
      if (!clickId) {
        clickId = nanoid(16);
      }
    }
  }

  const cookieData = {
    path: `/${encodeURI(originalKey)}`,
    dubIdCookieName,
    dubIdCookieValue: clickId,
    dubTestUrlValue: testUrl,
  };

  // only show inspect modal if the link is not password protected
  if (inspectMode && !password) {
    return NextResponse.rewrite(
      new URL(`/${domain}/${encodeURIComponent(key)}/inspect`, req.url),
      {
        headers: {
          ...DUB_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
      },
    );
  }

  // if the link is password protected
  if (password) {
    const pw =
      req.nextUrl.searchParams.get("pw") ||
      req.cookies.get(`dub_password_${linkId}`)?.value;

    // rewrite to auth page (/password/[domain]/[key]) if:
    // - no `pw` param is provided
    // - the `pw` param is incorrect
    // this will also ensure that no clicks are tracked unless the password is correct
    if (!pw || (await getLinkViaEdge({ domain, key }))?.password !== pw) {
      return NextResponse.rewrite(new URL(`/password/${linkId}`, req.url), {
        headers: {
          ...DUB_HEADERS,
          ...(!shouldIndex && {
            "X-Robots-Tag": "googlebot: noindex",
          }),
        },
      });
    } else if (pw) {
      // strip it from the URL if it's correct
      req.nextUrl.searchParams.delete("pw");
    }
  }

  // if the link is banned
  if (workspaceId === LEGAL_WORKSPACE_ID) {
    return NextResponse.rewrite(new URL(`/${domain}/banned`, req.url), {
      headers: {
        ...DUB_HEADERS,
        ...STATIC_PAGES_CACHE_HEADERS,
        "X-Robots-Tag": "googlebot: noindex",
      },
    });
  }

  // handle disabled links
  if (disabledAt) {
    return NextResponse.rewrite(new URL(`/${domain}/disabled`, req.url), {
      headers: {
        ...DUB_HEADERS,
        ...STATIC_PAGES_CACHE_HEADERS,
        "X-Robots-Tag": "googlebot: noindex",
      },
    });
  }

  // if the link has expired
  if (expiresAt && new Date(expiresAt) < new Date()) {
    if (expiredUrl) {
      return NextResponse.redirect(expiredUrl, {
        headers: {
          ...DUB_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
        status: 302,
      });
    } else {
      return NextResponse.rewrite(new URL(`/${domain}/expired`, req.url), {
        headers: {
          ...DUB_HEADERS,
          ...STATIC_PAGES_CACHE_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
      });
    }
  }

  // for Google click tracker domains (dub.sh, dub.link):
  // if there is a redirection url set, then use it instead of the target
  if (isGoogleClickTrackerDomain(domain)) {
    const redirectionUrl = searchParamsObj[REDIRECTION_QUERY_PARAM];

    // if a valid redirection url is present, return it immediately
    if (
      redirectionUrl &&
      isValidUrl(redirectionUrl) &&
      redirectionUrl.startsWith("https://")
    ) {
      ev.waitUntil(
        recordClick({
          req,
          clickId,
          workspaceId,
          linkId,
          domain,
          key,
          url: redirectionUrl,
          shouldCacheClickId,
        }),
      );

      return createResponseWithCookies(
        NextResponse.redirect(redirectionUrl, {
          headers: {
            ...DUB_HEADERS,
            ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
          },
          status: key === "_root" ? 301 : 302,
        }),
        cookieData,
      );
    }
  }

  // for root domain links, if there's no destination URL, rewrite to placeholder page
  if (!url) {
    ev.waitUntil(
      recordClick({
        req,
        clickId,
        workspaceId,
        linkId,
        domain,
        key,
        url,
        shouldCacheClickId,
      }),
    );

    const rewriteResponse = NextResponse.rewrite(
      new URL(`/${domain}`, req.url),
      {
        headers: {
          ...DUB_HEADERS,
          ...STATIC_PAGES_CACHE_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
      },
    );
    return createResponseWithCookies(rewriteResponse, cookieData);
  }

  const isBot = detectBot(req);
  const ua = userAgent(req);

  const { country } =
    process.env.VERCEL === "1" && geolocation(req)
      ? geolocation(req)
      : LOCALHOST_GEO_DATA;

  // rewrite to proxy page ([domain]/[key]/proxy) if it's a bot and proxy is enabled
  if (isBot && proxy) {
    return createResponseWithCookies(
      NextResponse.rewrite(
        new URL(`/${domain}/${encodeURIComponent(key)}/proxy`, req.url),
        {
          headers: {
            ...DUB_HEADERS,
            ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
          },
        },
      ),
      cookieData,
    );

    // rewrite to custom-uri-scheme page if the link is a custom URI scheme
  } else if (isSupportedCustomURIScheme(url)) {
    const finalUrl = getFinalUrl(url, {
      req,
      ...(shouldCacheClickId && { clickId }),
    });
    ev.waitUntil(
      recordClick({
        req,
        clickId,
        workspaceId,
        linkId,
        domain,
        key,
        url: finalUrl,
        shouldCacheClickId,
      }),
    );

    return createResponseWithCookies(
      NextResponse.rewrite(
        new URL(`/custom-uri-scheme/${encodeURIComponent(finalUrl)}`, req.url),
        {
          headers: {
            ...DUB_HEADERS,
            ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
          },
        },
      ),
      cookieData,
    );

    // rewrite to target URL if link cloaking is enabled
  } else if (rewrite) {
    const finalUrl = getFinalUrl(url, {
      req,
      ...(shouldCacheClickId && { clickId }),
    });
    ev.waitUntil(
      recordClick({
        req,
        clickId,
        workspaceId,
        linkId,
        domain,
        key,
        url: finalUrl,
        shouldCacheClickId,
      }),
    );

    return createResponseWithCookies(
      NextResponse.rewrite(
        new URL(`/cloaked/${encodeURIComponent(finalUrl)}`, req.url),
        {
          headers: {
            ...DUB_HEADERS,
            ...(!shouldIndex && {
              "X-Robots-Tag": "googlebot: noindex",
            }),
          },
        },
      ),
      cookieData,
    );

    // redirect to iOS link if it is specified and the user is on an iOS device
  } else if (ios && ua.os?.name === "iOS") {
    const finalUrl = getFinalUrl(ios, {
      req,
      ...(shouldCacheClickId && { clickId }),
    });
    ev.waitUntil(
      recordClick({
        req,
        clickId,
        workspaceId,
        linkId,
        domain,
        key,
        url: finalUrl,
        shouldCacheClickId,
      }),
    );

    // if it's an iOS app store URL (and skip_deeplink_preview is not set)
    // we need to show the interstitial page + cache deep link click data
    if (
      isIosAppStoreUrl(ios) &&
      !req.nextUrl.searchParams.get("skip_deeplink_preview")
    ) {
      if (clickId) {
        ev.waitUntil(
          cacheDeepLinkClickData({
            req,
            clickId,
            link: {
              id: linkId,
              domain,
              key,
              url, // pass the main destination URL to the cache (for deferred deep linking)
            },
          }),
        );
      }

      // redirect to the deeplink interstitial splash page "DeepLinkPreviewPage"
      // we're doing this because the interstitial page needs to be on a different domain than the actual deep link domain
      // @see: https://stackoverflow.com/a/78189982/10639526
      return createResponseWithCookies(
        NextResponse.redirect(
          new URL(`/deeplink/${domain}${fullPath}`, APP_DOMAIN),
          {
            headers: {
              ...DUB_HEADERS,
              ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
            },
          },
        ),
        cookieData,
      );
    }
    return createResponseWithCookies(
      NextResponse.redirect(finalUrl, {
        headers: {
          ...DUB_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
        status: key === "_root" ? 301 : 302,
      }),
      cookieData,
    );

    // redirect to Android link if it is specified and the user is on an Android device
  } else if (android && ua.os?.name === "Android") {
    const finalUrl = getFinalUrl(android, {
      req,
      ...(shouldCacheClickId && { clickId }),
    });
    ev.waitUntil(
      recordClick({
        req,
        clickId,
        workspaceId,
        linkId,
        domain,
        key,
        url: finalUrl,
        shouldCacheClickId,
      }),
    );

    // if it's a Google Play Store URL (and skip_deeplink_preview is not set)
    // we need to show the interstitial page + cache deep link click data
    if (
      isGooglePlayStoreUrl(android) &&
      !req.nextUrl.searchParams.get("skip_deeplink_preview")
    ) {
      if (clickId) {
        ev.waitUntil(
          cacheDeepLinkClickData({
            req,
            clickId,
            link: {
              id: linkId,
              domain,
              key,
              url, // pass the main destination URL to the cache (for deferred deep linking)
            },
          }),
        );
      }

      // redirect to the deeplink interstitial splash page "DeepLinkPreviewPage"
      return createResponseWithCookies(
        NextResponse.redirect(
          new URL(`/deeplink/${domain}${fullPath}`, APP_DOMAIN),
          {
            headers: {
              ...DUB_HEADERS,
              ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
            },
          },
        ),
        cookieData,
      );
    }

    return createResponseWithCookies(
      NextResponse.redirect(finalUrl, {
        headers: {
          ...DUB_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
        status: key === "_root" ? 301 : 302,
      }),
      cookieData,
    );

    // redirect to geo-targeting link if it is specified and the user is in the specified country
  } else if (geoTargeting && country && country in geoTargeting) {
    const finalUrl = getFinalUrl(geoTargeting[country], {
      req,
      ...(shouldCacheClickId && { clickId }),
    });
    ev.waitUntil(
      recordClick({
        req,
        clickId,
        workspaceId,
        linkId,
        domain,
        key,
        url: finalUrl,
        shouldCacheClickId,
      }),
    );

    return createResponseWithCookies(
      NextResponse.redirect(finalUrl, {
        headers: {
          ...DUB_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
        status: key === "_root" ? 301 : 302,
      }),
      cookieData,
    );

    // regular redirect
  } else {
    const finalUrl = getFinalUrl(url, {
      req,
      ...(shouldCacheClickId && { clickId }),
    });
    ev.waitUntil(
      recordClick({
        req,
        clickId,
        workspaceId,
        linkId,
        domain,
        key,
        url: finalUrl,
        shouldCacheClickId,
      }),
    );

    return createResponseWithCookies(
      NextResponse.redirect(finalUrl, {
        headers: {
          ...DUB_HEADERS,
          ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }),
        },
        status: key === "_root" ? 301 : 302,
      }),
      cookieData,
    );
  }
}