Skip to content

Commit 8aa8d16

Browse files
Miriaddashboard
andcommitted
fix: handle missing SANITY_API_TOKEN at build time
- Dashboard client gracefully handles missing token (returns null) - dashboardQuery returns empty results when client unavailable - All server actions check for client availability before mutations - Added force-dynamic to dashboard pages to prevent SSG data fetching - Prevents build errors when SANITY_API_TOKEN isn't set during build Co-authored-by: dashboard <dashboard@miriad.systems>
1 parent 4f8fcc8 commit 8aa8d16

File tree

8 files changed

+42
-8
lines changed

8 files changed

+42
-8
lines changed

app/(dashboard)/dashboard/content/actions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { revalidatePath } from "next/cache";
44
import { dashboardClient } from "@/lib/sanity/dashboard";
55

66
export async function approveIdea(id: string) {
7+
if (!dashboardClient) throw new Error("Sanity client not available");
78
await dashboardClient.patch(id).set({ status: "approved" }).commit();
89
revalidatePath("/dashboard/content");
910
}
1011

1112
export async function rejectIdea(id: string) {
13+
if (!dashboardClient) throw new Error("Sanity client not available");
1214
await dashboardClient.patch(id).set({ status: "rejected" }).commit();
1315
revalidatePath("/dashboard/content");
1416
}

app/(dashboard)/dashboard/content/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const dynamic = "force-dynamic";
2+
13
import { dashboardQuery } from "@/lib/sanity/dashboard";
24
import { ContentIdeasTable } from "./content-ideas-table";
35

app/(dashboard)/dashboard/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const dynamic = "force-dynamic";
2+
13
import { SectionCards } from "@/components/section-cards"
24

35
export default function DashboardPage() {

app/(dashboard)/dashboard/sponsors/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { revalidatePath } from "next/cache";
44
import { dashboardClient } from "@/lib/sanity/dashboard";
55

66
export async function updateLeadStatus(id: string, status: string) {
7+
if (!dashboardClient) throw new Error("Sanity client not available");
78
await dashboardClient.patch(id).set({ status }).commit();
89
revalidatePath("/dashboard/sponsors");
910
}

app/(dashboard)/dashboard/sponsors/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const dynamic = "force-dynamic";
2+
13
import { dashboardQuery } from "@/lib/sanity/dashboard";
24
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
35
import { SponsorLeadsTable } from "./sponsor-leads-table";

app/(dashboard)/dashboard/videos/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import { revalidatePath } from "next/cache";
44
import { dashboardClient } from "@/lib/sanity/dashboard";
55

66
export async function regenerateScript(id: string) {
7+
if (!dashboardClient) throw new Error("Sanity client not available");
78
await dashboardClient.patch(id).set({ status: "draft" }).commit();
89
revalidatePath("/dashboard/videos");
910
}
1011

1112
export async function retryRender(id: string) {
13+
if (!dashboardClient) throw new Error("Sanity client not available");
1214
await dashboardClient.patch(id).set({ status: "video_gen" }).commit();
1315
revalidatePath("/dashboard/videos");
1416
}
1517

1618
export async function publishAnyway(id: string) {
19+
if (!dashboardClient) throw new Error("Sanity client not available");
1720
await dashboardClient
1821
.patch(id)
1922
.set({ status: "published" })

app/(dashboard)/dashboard/videos/page.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const dynamic = "force-dynamic";
2+
13
import { dashboardQuery } from "@/lib/sanity/dashboard";
24
import { VideosTable } from "./videos-table";
35

lib/sanity/dashboard.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
1-
import { createClient } from "@sanity/client";
1+
import { createClient, type SanityClient } from "@sanity/client";
22
import { projectId, dataset, apiVersion } from "@/sanity/lib/api";
33

44
/**
55
* Server-side Sanity client for dashboard operations.
66
* Uses SANITY_API_TOKEN for authenticated read/write access.
77
* No CDN (fresh data), no stega (no preview encoding).
8+
*
9+
* Returns null if SANITY_API_TOKEN is not set (e.g., during build).
810
*/
9-
export const dashboardClient = createClient({
10-
projectId,
11-
dataset,
12-
apiVersion,
13-
useCdn: false,
14-
token: process.env.SANITY_API_TOKEN,
15-
});
11+
function createDashboardClient(): SanityClient | null {
12+
const token = process.env.SANITY_API_TOKEN;
13+
if (!token) {
14+
console.warn(
15+
"SANITY_API_TOKEN not set — dashboard queries will return empty results",
16+
);
17+
return null;
18+
}
19+
return createClient({
20+
projectId,
21+
dataset,
22+
apiVersion,
23+
useCdn: false,
24+
token,
25+
});
26+
}
27+
28+
export const dashboardClient = createDashboardClient();
1629

1730
/**
1831
* Fetch documents with a GROQ query using the dashboard client.
32+
* Returns empty/default result if client is not available.
1933
*/
2034
export async function dashboardQuery<T = unknown>(
2135
query: string,
2236
params?: Record<string, unknown>,
2337
): Promise<T> {
38+
if (!dashboardClient) {
39+
return (Array.isArray([] as unknown as T) ? [] : 0) as T;
40+
}
2441
return dashboardClient.fetch<T>(query, params ?? {});
2542
}
2643

@@ -31,5 +48,8 @@ export async function dashboardPatch(
3148
id: string,
3249
operations: Record<string, unknown>,
3350
) {
51+
if (!dashboardClient) {
52+
throw new Error("SANITY_API_TOKEN not set — cannot perform mutations");
53+
}
3454
return dashboardClient.patch(id).set(operations).commit();
3555
}

0 commit comments

Comments
 (0)