Skip to content

Commit

Permalink
web: redirect tags server side (#1287)
Browse files Browse the repository at this point in the history
* web: redirect tags server side

* refactor daimoLink parsing func
  • Loading branch information
andrewliu08 authored Aug 21, 2024
1 parent 30ba263 commit a1b2be5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 8 additions & 1 deletion apps/daimo-web/src/app/l/[[...slug]]/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
}

Expand Down
18 changes: 17 additions & 1 deletion packages/daimo-common/src/daimoLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit a1b2be5

Please sign in to comment.