Skip to content

Commit

Permalink
Remove iframeable check on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Dec 23, 2024
1 parent 37cfdc3 commit aab3379
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 133 deletions.
4 changes: 2 additions & 2 deletions apps/web/lib/api/links/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})),
Expand All @@ -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(
Expand Down
52 changes: 19 additions & 33 deletions apps/web/lib/middleware/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -102,7 +102,6 @@ export default async function LinkMiddleware(
trackConversion,
proxy,
rewrite,
iframeable,
expiresAt,
ios,
android,
Expand Down Expand Up @@ -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") {
Expand Down
1 change: 0 additions & 1 deletion apps/web/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export interface RedisLinkProps {
password?: boolean;
proxy?: boolean;
rewrite?: boolean;
iframeable?: boolean;
expiresAt?: Date;
expiredUrl?: string;
ios?: string;
Expand Down
7 changes: 1 addition & 6 deletions apps/web/lib/upstash/format-redis-link.ts
Original file line number Diff line number Diff line change
@@ -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<RedisLinkProps> {
export function formatRedisLink(link: ExpandedLink): RedisLinkProps {
const {
id,
domain,
url,
trackConversion,
password,
Expand All @@ -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 }),
Expand Down
74 changes: 0 additions & 74 deletions apps/web/scripts/update-noindex.ts

This file was deleted.

20 changes: 3 additions & 17 deletions apps/web/ui/modals/link-builder/options-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function LinkCloakingToggleBadge({
) : !data ? null : data?.iframeable ? (
<CheckCircleFill className="size-3.5 text-green-500" />
) : (
<AlertCircleFill className="size-3.5 text-yellow-500" />
<AlertCircleFill className="size-3.5 text-red-500" />
)
}
/>
Expand Down Expand Up @@ -158,22 +158,8 @@ function LinkCloakingToggleBadge({
</div>
) : (
<span>
We will try to cloak it with{" "}
<a
href="https://nextjs.org/docs/pages/api-reference/functions/next-response#rewrite"
target="_blank"
className="text-gray-500 underline underline-offset-2 hover:text-gray-700"
>
Next.js Rewrites
</a>
, but it might not work as expected.{" "}
<a
href="https://dub.co/help/article/link-cloaking"
target="_blank"
className="text-gray-500 underline underline-offset-2 hover:text-gray-700"
>
Learn more.
</a>
There are some issues with the link you provided. Please try
again.
</span>
)}
</div>
Expand Down

0 comments on commit aab3379

Please sign in to comment.