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.

Improve perf with dynamic = "error"

Steven Teyf3ec21f62d18
diff --git a/apps/web/app/api/links/[linkId]/archive/route.ts b/apps/web/app/api/links/[linkId]/archive/route.ts
index 4799b79d92..c58d433f10 100644
--- a/apps/web/app/api/links/[linkId]/archive/route.ts
+++ b/apps/web/app/api/links/[linkId]/archive/route.ts
@@ -2,13 +2,13 @@ import { archiveLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { NextResponse } from "next/server";
-// POST /api/projects/[slug]/links/[linkId]/archive – archive a link
+// POST /api/links/[linkId]/archive – archive a link
export const POST = withAuth(async ({ headers, link }) => {
const response = await archiveLink(link!.domain || "dub.sh", link!.key, true);
return NextResponse.json(response, { headers });
});
-// DELETE /api/projects/[slug]/links/[linkId]/archive – unarchive a link
+// DELETE /api/links/[linkId]/archive – unarchive a link
export const DELETE = withAuth(async ({ headers, link }) => {
const response = await archiveLink(
link!.domain || "dub.sh",
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index d80f49939c..1e78a7f335 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -3,7 +3,7 @@ import { deleteLink, editLink, processLink } from "@/lib/api/links";
import { NextResponse } from "next/server";
import { GOOGLE_FAVICON_URL, getApexDomain, log } from "@dub/utils";
-// PUT /api/projects/[slug]/links/[linkId] – update a link
+// PUT /api/links/[linkId] – update a link
export const PUT = withAuth(
async ({ req, headers, project, link, session }) => {
let body;
@@ -86,7 +86,7 @@ export const PUT = withAuth(
},
);
-// DELETE /api/projects/[slug]/links/[linkId] – delete a link
+// DELETE /api/links/[linkId] – delete a link
export const DELETE = withAuth(async ({ headers, link }) => {
// link is guaranteed to exist because if not we will return 404
const response = await deleteLink({
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.ts
index 086b3584e9..36667a5434 100644
--- a/apps/web/app/api/links/count/route.ts
+++ b/apps/web/app/api/links/count/route.ts
@@ -3,7 +3,7 @@ import { withAuth } from "@/lib/auth";
import { DUB_PROJECT_ID } from "@dub/utils";
import { NextResponse } from "next/server";
-// GET /api/projects/[slug]/links/count – get the number of links for a project
+// GET /api/links/count – get the number of links for a project
export const GET = withAuth(
async ({ headers, searchParams, project, session }) => {
const { userId } = searchParams;
diff --git a/apps/web/app/api/links/exists/route.ts b/apps/web/app/api/links/exists/route.ts
index 1cb8679cba..be1c94bf07 100644
--- a/apps/web/app/api/links/exists/route.ts
+++ b/apps/web/app/api/links/exists/route.ts
@@ -2,7 +2,7 @@ import { withAuth } from "@/lib/auth";
import { checkIfKeyExists } from "@/lib/api/links";
import { NextResponse } from "next/server";
-// GET /api/projects/[slug]/links/exists – check if a link exists
+// GET /api//links/exists – check if a link exists
export const GET = withAuth(async ({ headers, searchParams }) => {
const { domain, key } = searchParams;
const response = await checkIfKeyExists(domain || "dub.sh", key);
diff --git a/apps/web/app/api/links/info/route.ts b/apps/web/app/api/links/info/route.ts
index 32e29c73d2..d036ec8030 100644
--- a/apps/web/app/api/links/info/route.ts
+++ b/apps/web/app/api/links/info/route.ts
@@ -2,7 +2,7 @@ import { withAuth } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { NextResponse } from "next/server";
-// GET /api/projects/[slug]/links/info – get the info for a link
+// GET /api/links/info – get the info for a link
export const GET = withAuth(async ({ headers, searchParams }) => {
const { domain, key } = searchParams;
const response = await prisma.link.findUnique({
diff --git a/apps/web/app/api/links/random/route.ts b/apps/web/app/api/links/random/route.ts
index f973dff498..be9ffb941c 100644
--- a/apps/web/app/api/links/random/route.ts
+++ b/apps/web/app/api/links/random/route.ts
@@ -2,7 +2,7 @@ import { withAuth } from "@/lib/auth";
import { getRandomKey } from "@/lib/api/links";
import { NextResponse } from "next/server";
-// GET /api/projects/[slug]/links/random – get a random available link key
+// GET /api/links/random – get a random available link key
export const GET = withAuth(async ({ headers, searchParams }) => {
const { domain } = searchParams;
const response = await getRandomKey(domain || "dub.sh");
diff --git a/apps/web/app/api/projects/route.ts b/apps/web/app/api/projects/route.ts
index 2299f5643f..93559a9aba 100644
--- a/apps/web/app/api/projects/route.ts
+++ b/apps/web/app/api/projects/route.ts
@@ -19,6 +19,9 @@ export const GET = withAuth(async ({ session }) => {
},
},
},
+ include: {
+ domains: true,
+ },
});
return NextResponse.json(projects);
});
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/auth.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/client.tsx
similarity index 58%
rename from apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/auth.tsx
rename to apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/client.tsx
index 5b993b93f7..2c35c4ae8f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/auth.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/client.tsx
@@ -2,21 +2,26 @@
import useDomains from "@/lib/swr/use-domains";
import useProject from "@/lib/swr/use-project";
+import LayoutLoader from "@/ui/layout/layout-loader";
import LinkNotFound from "@/ui/links/link-not-found";
import ProjectExceededUsage from "@/ui/projects/project-exceeded-usage";
import { useSearchParams } from "next/navigation";
import { ReactNode } from "react";
-export default function AnalyticsAuth({ children }: { children: ReactNode }) {
- const { exceededUsage } = useProject();
+export default function AnalyticsClient({ children }: { children: ReactNode }) {
+ const { exceededUsage, loading } = useProject();
if (exceededUsage) {
return <ProjectExceededUsage />;
}
- const { domains, loading } = useDomains();
+ const { domains, loading: loadingDomains } = useDomains();
const searchParams = useSearchParams();
const domain = searchParams?.get("domain");
- if (!loading && domain && !domains?.find((d) => d.slug === domain)) {
+ if (loading || loadingDomains) {
+ return <LayoutLoader />;
+ }
+
+ if (domain && !domains?.find((d) => d.slug === domain)) {
return <LinkNotFound />;
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx
index 6dc6f605a5..484ea2cb54 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx
@@ -1,14 +1,14 @@
import Stats from "@/ui/stats";
-import AnalyticsAuth from "./auth";
+import AnalyticsClient from "./client";
import { Suspense } from "react";
import LayoutLoader from "@/ui/layout/layout-loader";
export default function ProjectAnalytics() {
return (
<Suspense fallback={<LayoutLoader />}>
- <AnalyticsAuth>
+ <AnalyticsClient>
<Stats />
- </AnalyticsAuth>
+ </AnalyticsClient>
</Suspense>
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/analytics/client.tsx b/apps/web/app/app.dub.co/(dashboard)/analytics/client.tsx
new file mode 100644
index 0000000000..f00a159acf
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/analytics/client.tsx
@@ -0,0 +1,40 @@
+"use client";
+
+import LayoutLoader from "@/ui/layout/layout-loader";
+import LinkNotFound from "@/ui/links/link-not-found";
+import { fetcher } from "@dub/utils";
+import { useSession } from "next-auth/react";
+import { useParams, useSearchParams } from "next/navigation";
+import { ReactNode } from "react";
+import useSWR from "swr";
+
+export default function AnalyticsClient({ children }: { children: ReactNode }) {
+ const searchParams = useSearchParams();
+ const domain = searchParams?.get("domain");
+ const key = searchParams?.get("key");
+
+ if (domain !== "dub.sh" || !key) {
+ return <LinkNotFound />;
+ }
+
+ const { slug } = useParams() as { slug?: string };
+ const { data: session, status } = useSession();
+ const { data: link, isLoading } = useSWR(
+ `/api/links/info?domain=${domain}&key=${key}${
+ slug ? `&projectSlug=${slug}` : ""
+ }`,
+ fetcher,
+ { dedupingInterval: 30000 },
+ );
+
+ if (status === "loading" || isLoading) {
+ return <LayoutLoader />;
+ }
+
+ // @ts-expect-error
+ if (session?.user?.id !== link?.userId) {
+ return <LinkNotFound />;
+ }
+
+ return children;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/analytics/page.tsx b/apps/web/app/app.dub.co/(dashboard)/analytics/page.tsx
index d1acebe774..47ca3b683f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/analytics/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/analytics/page.tsx
@@ -1,29 +1,14 @@
-import { getSession } from "@/lib/auth";
-import { getLink } from "@/lib/fetchers";
-import LinkNotFound from "@/ui/links/link-not-found";
import Stats from "@/ui/stats";
-import { cache } from "react";
+import { Suspense } from "react";
+import AnalyticsClient from "./client";
+import LayoutLoader from "@/ui/layout/layout-loader";
-const cachedGetSession = cache(getSession);
-
-export default async function LinkAnalytics({
- searchParams,
-}: {
- searchParams: { [key: string]: string | undefined };
-}) {
- if (searchParams.domain !== "dub.sh" || !searchParams?.key) {
- return <LinkNotFound />;
- }
- const [session, link] = await Promise.all([
- cachedGetSession(),
- getLink({
- domain: searchParams.domain || "dub.sh",
- key: searchParams.key || "github",
- }),
- ]);
-
- if (session?.user?.id !== link?.userId) {
- return <LinkNotFound />;
- }
- return <Stats />;
+export default function LinkAnalytics() {
+ return (
+ <Suspense fallback={<LayoutLoader />}>
+ <AnalyticsClient>
+ <Stats />
+ </AnalyticsClient>
+ </Suspense>
+ );
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
index 2c133e7b00..2910c4b3b3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/layout.tsx
@@ -6,10 +6,11 @@ import Link from "next/link";
import { ReactNode, Suspense } from "react";
import UserDropdown from "@/ui/layout/user-dropdown";
import ProjectSelect from "@/ui/layout/project-select";
-import ProjectSelectPlaceholder from "@/ui/layout/project-select/placeholder";
import NavTabs from "@/ui/layout/nav-tabs";
import UpgradeBanner from "@/ui/layout/upgrade-banner";
+export const dynamic = "error";
+export const dynamicParams = true;
export const metadata = constructMetadata();
export default function Layout({ children }: { children: ReactNode }) {
@@ -23,9 +24,7 @@ export default function Layout({ children }: { children: ReactNode }) {
<Logo className="h-8 w-8 transition-all duration-75 active:scale-95" />
</Link>
<Divider className="h-8 w-8 text-gray-200 sm:ml-3" />
- <Suspense fallback={<ProjectSelectPlaceholder />}>
- <ProjectSelect />
- </Suspense>
+ <ProjectSelect />
<UpgradeBanner />
</div>
<div className="flex items-center space-x-6">
diff --git a/apps/web/app/app.dub.co/(dashboard)/page.tsx b/apps/web/app/app.dub.co/(dashboard)/page.tsx
index 50d41be793..bde35e527c 100644
--- a/apps/web/app/app.dub.co/(dashboard)/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/page.tsx
@@ -1,7 +1,5 @@
import CreateProjectButton from "@/ui/projects/create-project-button";
-import ProjectCardPlaceholder from "@/ui/projects/project-card-placeholder";
import { MaxWidthWrapper } from "@dub/ui";
-import { Suspense } from "react";
import ProjectList from "@/ui/projects/project-list";
export default function App() {
@@ -17,13 +15,7 @@ export default function App() {
</div>
<MaxWidthWrapper>
<div className="my-10 grid grid-cols-1 gap-5 lg:grid-cols-2 xl:grid-cols-3">
- <Suspense
- fallback={Array.from({ length: 6 }).map((_, i) => (
- <ProjectCardPlaceholder key={i} />
- ))}
- >
- <ProjectList />
- </Suspense>
+ <ProjectList />
</div>
</MaxWidthWrapper>
</>
diff --git a/apps/web/lib/swr/use-projects.ts b/apps/web/lib/swr/use-projects.ts
new file mode 100644
index 0000000000..a72f14bfbd
--- /dev/null
+++ b/apps/web/lib/swr/use-projects.ts
@@ -0,0 +1,23 @@
+import { ProjectWithDomainProps } from "@/lib/types";
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+
+export default function useProjects() {
+ const { data: projects, error } = useSWR<ProjectWithDomainProps[]>(
+ "/api/projects",
+ fetcher,
+ {
+ dedupingInterval: 30000,
+ },
+ );
+
+ return {
+ projects: projects?.map((project) => ({
+ ...project,
+ primaryDomain:
+ project.domains.find((domain) => domain.primary) || project.domains[0],
+ })),
+ error,
+ loading: !projects && !error,
+ };
+}
diff --git a/apps/web/ui/layout/project-select/client.tsx b/apps/web/ui/layout/project-select.tsx
similarity index 91%
rename from apps/web/ui/layout/project-select/client.tsx
rename to apps/web/ui/layout/project-select.tsx
index 6f3822d445..a8b22a583f 100644
--- a/apps/web/ui/layout/project-select/client.tsx
+++ b/apps/web/ui/layout/project-select.tsx
@@ -10,13 +10,10 @@ import { useSession } from "next-auth/react";
import Link from "next/link";
import { useParams, usePathname } from "next/navigation";
import { useCallback, useContext, useMemo, useState } from "react";
-import ProjectSelectPlaceholder from "./placeholder";
+import useProjects from "@/lib/swr/use-projects";
-export default function ProjectSelectClient({
- projects,
-}: {
- projects: ProjectWithDomainProps[];
-}) {
+export default function ProjectSelect() {
+ const { projects } = useProjects();
const { data: session, status } = useSession();
const { slug, key } = useParams() as {
slug?: string;
@@ -55,7 +52,7 @@ export default function ProjectSelectClient({
const [openPopover, setOpenPopover] = useState(false);
- if (status === "loading") {
+ if (!projects || status === "loading") {
return <ProjectSelectPlaceholder />;
}
@@ -205,3 +202,13 @@ function ProjectList({
</div>
);
}
+
+function ProjectSelectPlaceholder() {
+ return (
+ <div className="flex animate-pulse items-center space-x-1.5 rounded-lg px-1.5 py-2 sm:w-60">
+ <div className="h-8 w-8 animate-pulse rounded-full bg-gray-200" />
+ <div className="hidden h-8 w-28 animate-pulse rounded-md bg-gray-200 sm:block sm:w-40" />
+ <ChevronsUpDown className="h-4 w-4 text-gray-400" aria-hidden="true" />
+ </div>
+ );
+}
diff --git a/apps/web/ui/layout/project-select/index.tsx b/apps/web/ui/layout/project-select/index.tsx
deleted file mode 100644
index 30b57865f1..0000000000
--- a/apps/web/ui/layout/project-select/index.tsx
+++ /dev/null
@@ -1,7 +0,0 @@
-import { getProjects } from "@/lib/fetchers";
-import ProjectSelectClient from "./client";
-
-export default async function ProjectSelect() {
- const projects = await getProjects();
- return <ProjectSelectClient projects={projects || []} />;
-}
diff --git a/apps/web/ui/layout/project-select/placeholder.tsx b/apps/web/ui/layout/project-select/placeholder.tsx
deleted file mode 100644
index 1919a33a6f..0000000000
--- a/apps/web/ui/layout/project-select/placeholder.tsx
+++ /dev/null
@@ -1,11 +0,0 @@
-import { ChevronsUpDown } from "lucide-react";
-
-export default function ProjectSelectPlaceholder() {
- return (
- <div className="flex animate-pulse items-center space-x-1.5 rounded-lg px-1.5 py-2 sm:w-60">
- <div className="h-8 w-8 animate-pulse rounded-full bg-gray-200" />
- <div className="hidden h-8 w-28 animate-pulse rounded-md bg-gray-200 sm:block sm:w-40" />
- <ChevronsUpDown className="h-4 w-4 text-gray-400" aria-hidden="true" />
- </div>
- );
-}
diff --git a/apps/web/ui/projects/project-list.tsx b/apps/web/ui/projects/project-list.tsx
index 205ee115dd..f8894b4011 100644
--- a/apps/web/ui/projects/project-list.tsx
+++ b/apps/web/ui/projects/project-list.tsx
@@ -1,9 +1,18 @@
-import { getProjects } from "@/lib/fetchers";
-import NoProjectsPlaceholder from "@/ui/projects/no-projects-placeholder";
+"use client";
+
+import useProjects from "@/lib/swr/use-projects";
import ProjectCard from "@/ui/projects/project-card";
+import ProjectCardPlaceholder from "./project-card-placeholder";
+import NoProjectsPlaceholder from "@/ui/projects/no-projects-placeholder";
-export default async function ProjectList() {
- const projects = await getProjects();
+export default function ProjectList() {
+ const { projects, loading } = useProjects();
+
+ if (loading) {
+ return Array.from({ length: 6 }).map((_, i) => (
+ <ProjectCardPlaceholder key={i} />
+ ));
+ }
if (!projects || projects.length === 0) {
return <NoProjectsPlaceholder />;