Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove iframeable check on demand #1831

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

68 changes: 23 additions & 45 deletions apps/web/ui/modals/link-builder/options-list.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -121,10 +121,10 @@ function LinkCloakingToggleBadge({
icon={
isLoading ? (
<LoadingSpinner className="size-3.5" />
) : !data ? null : data?.iframeable ? (
) : !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-amber-500" />
)
}
/>
Expand All @@ -135,48 +135,26 @@ function LinkCloakingToggleBadge({
return data ? (
<Tooltip
content={
<div
className={cn(
"block max-w-lg text-pretty p-4 text-center text-sm text-gray-700",
{
"max-w-sm": !data.iframeable,
},
)}
>
{data.iframeable ? (
<div className="grid gap-2">
<div className="h-[250px] w-[444px] overflow-hidden rounded-lg border border-gray-200">
<iframe
src={url}
style={{
zoom: 0.5,
}}
className="h-[500px] w-[888px]"
/>
</div>
<p>Your link will be successfully cloaked.</p>
data.iframeable ? (
<div className="grid max-w-lg gap-2 text-pretty p-4 text-center text-sm text-gray-700">
<div className="h-[250px] w-[444px] overflow-hidden rounded-lg border border-gray-200">
<iframe
src={url}
style={{
zoom: 0.5,
}}
className="h-[500px] w-[888px]"
/>
</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>
</span>
)}
</div>
<p>Your link will be successfully cloaked.</p>
</div>
) : (
<SimpleTooltipContent
title="Your link is not cloakable – make sure you have the right security headers set on your target URL."
cta="Learn more"
href="https://dub.co/help/article/link-cloaking#link-cloaking-with-security-headers"
/>
)
}
>
<div>{badge}</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/functions/is-iframeable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ export const isIframeable = async ({
return false;
}

return false;
return true;
};
Loading