From a1b2be582b8981d46a0fd8508d281d30b2d92a72 Mon Sep 17 00:00:00 2001 From: andrewliu08 <55035762+andrewliu08@users.noreply.github.com> Date: Tue, 20 Aug 2024 22:48:35 -0700 Subject: [PATCH] web: redirect tags server side (#1287) * web: redirect tags server side * refactor daimoLink parsing func --- apps/daimo-web/src/app/l/[[...slug]]/page.tsx | 9 ++++++++- packages/daimo-common/src/daimoLink.ts | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/apps/daimo-web/src/app/l/[[...slug]]/page.tsx b/apps/daimo-web/src/app/l/[[...slug]]/page.tsx index 2250592ea..155544cef 100644 --- a/apps/daimo-web/src/app/l/[[...slug]]/page.tsx +++ b/apps/daimo-web/src/app/l/[[...slug]]/page.tsx @@ -1,6 +1,7 @@ /* eslint-disable @next/next/no-img-element */ -import { debugJson } from "@daimo/common"; +import { debugJson, formatDaimoLink, parseDaimoLinkType } from "@daimo/common"; import { Metadata } from "next"; +import { redirect } from "next/navigation"; import LinkPage from "./LinkPage"; import { getUrl, LinkPageProps } from "./LinkPageProps"; @@ -25,7 +26,13 @@ export async function generateMetadata( const i18n = getI18N(getReqLang()); const url = getUrl(props); const desc = await loadLinkStatusDesc(url); + if (desc == null) return defaultMeta(i18n); + if (parseDaimoLinkType(url) === "t" && desc?.linkStatus?.link) { + console.log(`[LINK] redirecting tag to ${desc.linkStatus.link}`); + redirect(formatDaimoLink(desc.linkStatus.link)); + } + return createMetadataForLinkStatus(desc); } diff --git a/packages/daimo-common/src/daimoLink.ts b/packages/daimo-common/src/daimoLink.ts index 0b2a14cd1..4b5b3a018 100644 --- a/packages/daimo-common/src/daimoLink.ts +++ b/packages/daimo-common/src/daimoLink.ts @@ -197,7 +197,15 @@ export function parseDaimoLink(link: string): DaimoLink | null { } } -function parseDaimoLinkInner(link: string): DaimoLink | null { +export function parseDaimoLinkType(link: string): string | null { + const parsedLink = parseDaimoLinkUrl(link); + if (parsedLink == null) return null; + + const { parts } = parsedLink; + return parts[0]; +} + +function parseDaimoLinkUrl(link: string): { url: URL; parts: string[] } | null { const prefixes = [ `${daimoLinkBase}/`, `${daimoLinkBaseV2}/`, // New shorter link prefix @@ -212,6 +220,14 @@ function parseDaimoLinkInner(link: string): DaimoLink | null { const suffix = url.pathname.substring(1); const parts = suffix.split("/"); + return { url, parts }; +} + +function parseDaimoLinkInner(link: string): DaimoLink | null { + const parsedLink = parseDaimoLinkUrl(link); + if (parsedLink == null) return null; + const { url, parts } = parsedLink; + switch (parts[0]) { case "account": { if (parts.length !== 2) return null;