From aab3379fd568e3635055f2d70603c9182b70aaf1 Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Mon, 23 Dec 2024 11:24:23 -0800 Subject: [PATCH 1/2] Remove iframeable check on demand --- apps/web/lib/api/links/cache.ts | 4 +- apps/web/lib/middleware/link.ts | 52 +++++-------- apps/web/lib/types.ts | 1 - apps/web/lib/upstash/format-redis-link.ts | 7 +- apps/web/scripts/update-noindex.ts | 74 ------------------- .../ui/modals/link-builder/options-list.tsx | 20 +---- 6 files changed, 25 insertions(+), 133 deletions(-) delete mode 100644 apps/web/scripts/update-noindex.ts diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts index 3fb70818ee..93c69c9697 100644 --- a/apps/web/lib/api/links/cache.ts +++ b/apps/web/lib/api/links/cache.ts @@ -19,7 +19,7 @@ class LinkCache { const redisLinks = await Promise.all( links.map(async (link) => ({ - ...(await formatRedisLink(link)), + ...formatRedisLink(link), key: link.key.toLowerCase(), domain: link.domain.toLowerCase(), })), @@ -40,7 +40,7 @@ class LinkCache { } async set(link: ExpandedLink) { - const redisLink = await formatRedisLink(link); + const redisLink = formatRedisLink(link); const hasWebhooks = redisLink.webhookIds && redisLink.webhookIds.length > 0; return await redis.set( diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts index f2e1235cc2..0b08a78b31 100644 --- a/apps/web/lib/middleware/link.ts +++ b/apps/web/lib/middleware/link.ts @@ -90,7 +90,7 @@ export default async function LinkMiddleware( } // format link to fit the RedisLinkProps interface - link = await formatRedisLink(linkData as any); + link = formatRedisLink(linkData as any); ev.waitUntil(linkCache.set(linkData as any)); } @@ -102,7 +102,6 @@ export default async function LinkMiddleware( trackConversion, proxy, rewrite, - iframeable, expiresAt, ios, android, @@ -280,41 +279,28 @@ export default async function LinkMiddleware( }), ); - if (iframeable) { - return createResponseWithCookie( - NextResponse.rewrite( - new URL( - `/cloaked/${encodeURIComponent( - getFinalUrl(url, { - req, - clickId: trackConversion ? clickId : undefined, - }), - )}`, - req.url, - ), - { - headers: { - ...DUB_HEADERS, - ...(!shouldIndex && { - "X-Robots-Tag": "googlebot: noindex", - }), - }, - }, + return createResponseWithCookie( + NextResponse.rewrite( + new URL( + `/cloaked/${encodeURIComponent( + getFinalUrl(url, { + req, + clickId: trackConversion ? clickId : undefined, + }), + )}`, + req.url, ), - { clickId, path: `/${originalKey}` }, - ); - } else { - // if link is not iframeable, use Next.js rewrite instead - return createResponseWithCookie( - NextResponse.rewrite(url, { + { headers: { ...DUB_HEADERS, - ...(!shouldIndex && { "X-Robots-Tag": "googlebot: noindex" }), + ...(!shouldIndex && { + "X-Robots-Tag": "googlebot: noindex", + }), }, - }), - { clickId, path: `/${originalKey}` }, - ); - } + }, + ), + { clickId, path: `/${originalKey}` }, + ); // redirect to iOS link if it is specified and the user is on an iOS device } else if (ios && userAgent(req).os?.name === "iOS") { diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts index daac0287b7..2d67305daf 100644 --- a/apps/web/lib/types.ts +++ b/apps/web/lib/types.ts @@ -85,7 +85,6 @@ export interface RedisLinkProps { password?: boolean; proxy?: boolean; rewrite?: boolean; - iframeable?: boolean; expiresAt?: Date; expiredUrl?: string; ios?: string; diff --git a/apps/web/lib/upstash/format-redis-link.ts b/apps/web/lib/upstash/format-redis-link.ts index 6c20c20bb5..986ba656d7 100644 --- a/apps/web/lib/upstash/format-redis-link.ts +++ b/apps/web/lib/upstash/format-redis-link.ts @@ -1,13 +1,9 @@ -import { isIframeable } from "@dub/utils"; import { ExpandedLink } from "../api/links/utils/transform-link"; import { RedisLinkProps } from "../types"; -export async function formatRedisLink( - link: ExpandedLink, -): Promise { +export function formatRedisLink(link: ExpandedLink): RedisLinkProps { const { id, - domain, url, trackConversion, password, @@ -34,7 +30,6 @@ export async function formatRedisLink( ...(url && rewrite && { rewrite: true, - iframeable: await isIframeable({ url, requestDomain: domain }), }), ...(expiresAt && { expiresAt: new Date(expiresAt) }), ...(expiredUrl && { expiredUrl }), diff --git a/apps/web/scripts/update-noindex.ts b/apps/web/scripts/update-noindex.ts deleted file mode 100644 index 9db9da39e9..0000000000 --- a/apps/web/scripts/update-noindex.ts +++ /dev/null @@ -1,74 +0,0 @@ -// @ts-nocheck – old migration script - -import { redis } from "@/lib/upstash"; -import { prisma } from "@dub/prisma"; -import { isIframeable } from "@dub/utils"; -import "dotenv-flow/config"; - -async function main() { - const domains = await prisma.domain.findMany({ - where: { - AND: [ - { - target: { - not: null, - }, - }, - { - target: { - not: "", - }, - }, - ], - noindex: false, - }, - take: 100, - }); - - // const res = await Promise.all( - // domains.map(async (d) => { - // return await redis.hget(d.slug.toLowerCase(), "_root"); - // }), - // ); - - // console.log({ res }); - - const res = await Promise.all( - domains.map(async (d) => { - const { slug: domain, id, target: url, type, projectId } = d; - const rewrite = type === "rewrite"; - - return await redis - .hset(domain.toLowerCase(), { - _root: { - id, - url, - ...(url && - rewrite && { - rewrite: true, - iframeable: await isIframeable({ - url, - requestDomain: domain.toLowerCase(), - }), - }), - noindex: true, - projectId, - }, - }) - .then(async () => { - return await prisma.domain.update({ - where: { - id, - }, - data: { - noindex: true, - }, - }); - }); - }), - ); - - console.log(res); -} - -main(); diff --git a/apps/web/ui/modals/link-builder/options-list.tsx b/apps/web/ui/modals/link-builder/options-list.tsx index 2a57062257..549e096a8e 100644 --- a/apps/web/ui/modals/link-builder/options-list.tsx +++ b/apps/web/ui/modals/link-builder/options-list.tsx @@ -124,7 +124,7 @@ function LinkCloakingToggleBadge({ ) : !data ? null : data?.iframeable ? ( ) : ( - + ) } /> @@ -158,22 +158,8 @@ function LinkCloakingToggleBadge({ ) : ( - We will try to cloak it with{" "} - - Next.js Rewrites - - , but it might not work as expected.{" "} - - Learn more. - + There are some issues with the link you provided. Please try + again. )} From d9501de346a82483877da0fbfaf778a6117e5eac Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Mon, 23 Dec 2024 11:40:30 -0800 Subject: [PATCH 2/2] fix iframeable --- .../ui/modals/link-builder/options-list.tsx | 54 ++++++++----------- packages/utils/src/functions/is-iframeable.ts | 2 +- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/apps/web/ui/modals/link-builder/options-list.tsx b/apps/web/ui/modals/link-builder/options-list.tsx index 549e096a8e..406fefadf3 100644 --- a/apps/web/ui/modals/link-builder/options-list.tsx +++ b/apps/web/ui/modals/link-builder/options-list.tsx @@ -1,7 +1,7 @@ import { AlertCircleFill, CheckCircleFill, X } from "@/ui/shared/icons"; -import { Tooltip, useMediaQuery } from "@dub/ui"; +import { SimpleTooltipContent, Tooltip, useMediaQuery } from "@dub/ui"; import { LoadingSpinner } from "@dub/ui/icons"; -import { cn, fetcher, isValidUrl as isValidUrlFn } from "@dub/utils"; +import { fetcher, isValidUrl as isValidUrlFn } from "@dub/utils"; import { AnimatePresence, motion } from "framer-motion"; import { ReactNode, useMemo } from "react"; import { useFormContext } from "react-hook-form"; @@ -121,10 +121,10 @@ function LinkCloakingToggleBadge({ icon={ isLoading ? ( - ) : !data ? null : data?.iframeable ? ( + ) : !data ? null : data.iframeable ? ( ) : ( - + ) } /> @@ -135,34 +135,26 @@ function LinkCloakingToggleBadge({ return data ? ( - {data.iframeable ? ( -
-
-