Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

don't fetch embed API routes if not in viewport

Steven Tey00a588a8c493
diff --git a/apps/web/app/app.dub.co/embed/inline/page-client.tsx b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
index 7b4e56d9ea..b133532312 100644
--- a/apps/web/app/app.dub.co/embed/inline/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/inline/page-client.tsx
@@ -10,11 +10,12 @@ import {
Copy,
MoneyBill2,
useCopyToClipboard,
+ useInViewport,
Wordmark,
} from "@dub/ui";
import { cn, fetcher, getPrettyUrl } from "@dub/utils";
import { Link, Program } from "@prisma/client";
-import { CSSProperties } from "react";
+import { CSSProperties, useRef } from "react";
import useSWR from "swr";
import { Activity } from "../activity";
import { SalesList } from "../sales-list";
@@ -33,13 +34,17 @@ export function EmbedInlinePageClient({
}) {
const [copied, copyToClipboard] = useCopyToClipboard();
+ const divRef = useRef<HTMLDivElement>(null);
+ const isVisible = useInViewport(divRef);
+
const { data: sales, isLoading } = useSWR<PartnerSaleResponse[]>(
- "/api/embed/sales",
+ isVisible && "/api/embed/sales",
fetcher,
);
return (
<div
+ ref={divRef}
className="flex min-h-screen flex-col"
style={
{ "--accent-color": program.brandColor || "#171717" } as CSSProperties
@@ -142,7 +147,7 @@ export function EmbedInlinePageClient({
))}
</div>
</>
- <LinkToken />
+ {isVisible && <LinkToken />}
</div>
</div>
<div className="flex grow flex-col justify-end">
diff --git a/apps/web/app/app.dub.co/embed/widget/page-client.tsx b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
index a8d2ade260..2e19815965 100644
--- a/apps/web/app/app.dub.co/embed/widget/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/widget/page-client.tsx
@@ -7,6 +7,7 @@ import {
buttonVariants,
ToggleGroup,
useCopyToClipboard,
+ useInViewport,
Wordmark,
} from "@dub/ui";
import {
@@ -21,7 +22,7 @@ import {
import { cn, fetcher, getPrettyUrl } from "@dub/utils";
import { Link, Program } from "@prisma/client";
import { motion } from "framer-motion";
-import { CSSProperties, useState } from "react";
+import { CSSProperties, useRef, useState } from "react";
import useSWR from "swr";
import { Activity } from "../activity";
import { SalesList } from "../sales-list";
@@ -45,13 +46,17 @@ export function EmbedWidgetPageClient({
const [copied, copyToClipboard] = useCopyToClipboard();
const [selectedTab, setSelectedTab] = useState<Tab>("invite");
+ const divRef = useRef<HTMLDivElement>(null);
+ const isVisible = useInViewport(divRef);
+
const { data: sales, isLoading } = useSWR<PartnerSaleResponse[]>(
- "/api/embed/sales",
+ isVisible && "/api/embed/sales",
fetcher,
);
return (
<div
+ ref={divRef}
className="flex min-h-screen flex-col"
style={
{ "--accent-color": program.brandColor || "#171717" } as CSSProperties
@@ -256,7 +261,7 @@ export function EmbedWidgetPageClient({
</motion.div>
</>
)}
- <LinkToken />
+ {isVisible && <LinkToken />}
</div>
</div>
<div className="flex grow flex-col justify-end">