From 84ef3dbeb526de14f58a28ba302278418980b667 Mon Sep 17 00:00:00 2001 From: Jared Schoeny Date: Mon, 19 Oct 2026 15:59:50 -0600 Subject: [PATCH] Stop Supabase outages from caching hack page 404s (#112) Co-authored-by: Claude Opus 5.5 --- src/app/api/refresh/route.ts | 28 +++++++++++++ src/app/hack/[slug]/error.tsx | 41 +++++++++++++++++++ src/app/hack/[slug]/metadata.ts | 37 ++++++++++++----- .../patches/patcher-selectable-patches.ts | 22 +++------- 4 files changed, 101 insertions(+), 27 deletions(-) create mode 100644 src/app/api/refresh/route.ts create mode 100644 src/app/hack/[slug]/error.tsx diff --git a/src/app/api/refresh/route.ts b/src/app/api/refresh/route.ts new file mode 100644 index 0000000..dc8a9c7 --- /dev/null +++ b/src/app/api/refresh/route.ts @@ -0,0 +1,28 @@ +import { NextRequest, NextResponse } from "next/server"; +import { revalidatePath, revalidateTag } from "next/cache"; +import { checkUserRoles } from "@/utils/user"; +import { createClient } from "@/utils/supabase/server"; +import { revalidateDiscoverCatalog } from "@/app/discover/revalidate"; + +/** + * Admin-only: drops the cached data and rendered pages for every hack, plus + * the discover catalog. Use after a Supabase outage left bad pages cached. + * For a single hack, use /api/refresh/[slug]. + */ +export async function GET(req: NextRequest) { + const supa = await createClient(); + const { data: user } = await supa.auth.getUser(); + if (!user) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + const { isAdmin } = await checkUserRoles(supa); + if (!isAdmin) { + return NextResponse.json({ error: "Forbidden" }, { status: 403 }); + } + + // Every hack cache entry (metadata, downloads, redirect) carries the "hack" tag. + revalidateTag("hack"); + revalidatePath("/hack/[slug]", "layout"); + revalidateDiscoverCatalog(); + return NextResponse.redirect(new URL("/discover", req.url)); +} diff --git a/src/app/hack/[slug]/error.tsx b/src/app/hack/[slug]/error.tsx new file mode 100644 index 0000000..0893028 --- /dev/null +++ b/src/app/hack/[slug]/error.tsx @@ -0,0 +1,41 @@ +"use client"; + +import Link from "next/link"; +import { useRouter } from "next/navigation"; +import { startTransition } from "react"; + +/** Shown when hack data fails to load (usually a Supabase outage), so it doesn't read as a missing hack. */ +export default function HackErrorPage({ reset }: { reset: () => void }) { + const router = useRouter(); + + function retry() { + startTransition(() => { + router.refresh(); + reset(); + }); + } + + return ( +
+
+

Couldn't load this hack

+

Something went wrong loading the hack data. Try again in a moment.

+
+ + + View all hacks + +
+
+
+ ); +} diff --git a/src/app/hack/[slug]/metadata.ts b/src/app/hack/[slug]/metadata.ts index 7a391dd..8f46fca 100644 --- a/src/app/hack/[slug]/metadata.ts +++ b/src/app/hack/[slug]/metadata.ts @@ -12,6 +12,12 @@ import type { SelectablePatch } from "@/types/patcher"; // stay out of "use server" files: every export there is a public endpoint that // anyone can call directly, which leaked private drafts and creator emails. // Callers gate what they render (drafts, admin-only email and contact). +// +// Every Supabase error throws instead of falling back to null or empty data. +// unstable_cache doesn't store rejections, so an outage can't get cached as a +// missing hack (404) or a page with no covers; the next request after Supabase +// recovers loads fresh data, and hack-page renders that throw keep serving the +// last good ISR page (or the error boundary if there isn't one yet). export interface HackMetadata { hack: { @@ -67,7 +73,7 @@ export interface HackMetadata { }; } -/** null for deleted hacks too; pages that get null should call `hackNotFound`. */ +/** null for missing and deleted hacks; pages that get null should call `hackNotFound`. Throws on Supabase errors. */ export async function getHackMetadata(slug: string): Promise { const runner = cache( async () => { @@ -80,7 +86,8 @@ export async function getHackMetadata(slug: string): Promise 0) { images = getCoverUrls(covers.map(c => c.url)); } // Fetch tags - const { data: tagRows } = await supabase + const { data: tagRows, error: tagsError } = await supabase .from("hack_tags") .select("order,tags(name)") .eq("hack_slug", slug); + if (tagsError) throw tagsError; const tags = sortOrderedTags( (tagRows || []) @@ -113,16 +122,18 @@ export async function getHackMetadata(slug: string): Promise t.name); // Fetch profile - const { data: profile } = await supabase + const { data: profile, error: profileError } = await supabase .from("profiles") .select("id,username,avatar_url,verified") .eq("id", hack.created_by as string) .maybeSingle(); + if (profileError) throw profileError; // Meant to only be available to admins (gated in server-side page rendering) let userEmail: string | null = null; if (profile) { - const { data: userData } = await supabase.auth.admin.getUserById(profile.id); + const { data: userData, error: userError } = await supabase.auth.admin.getUserById(profile.id); + if (userError) throw userError; userEmail = userData?.user?.email || null; } @@ -133,7 +144,7 @@ export async function getHackMetadata(slug: string): Promise { .select("downloads") .eq("slug", slug) .maybeSingle(); - - if (error || !hack) return null; + + if (error) throw error; + if (!hack) return null; return hack.downloads || 0; }, [`hack:${slug}:downloads`], @@ -241,12 +255,13 @@ export async function hackNotFound(slug: string): Promise { const runner = cache( async () => { const supabase = await createServiceClient(); - const { data } = await supabase + const { data, error } = await supabase .from("hacks") .select("redirect_url") .eq("slug", slug) .not("deleted_at", "is", null) .maybeSingle(); + if (error) throw error; return data?.redirect_url ?? null; }, [`hack:${slug}:redirect`], diff --git a/src/utils/patches/patcher-selectable-patches.ts b/src/utils/patches/patcher-selectable-patches.ts index 77ca6c2..9a5a690 100644 --- a/src/utils/patches/patcher-selectable-patches.ts +++ b/src/utils/patches/patcher-selectable-patches.ts @@ -2,6 +2,10 @@ import type { Database } from "@/types/db"; import type { PatcherPatchSelection, SelectablePatch } from "@/types/patcher"; import type { SupabaseClient } from "@supabase/supabase-js"; +/** + * Patches the patcher offers for a hack, plus the default pick. Throws on + * Supabase errors so callers never mistake an outage for "no patches". + */ export async function getPatcherSelectablePatches( supabase: SupabaseClient, slug: string, @@ -14,14 +18,7 @@ export async function getPatcherSelectablePatches( .eq("patches.published", true) .eq("patches.archived", false) .order("sort_order", { ascending: true }); - if (error) { - console.error(error); - return { - savedPatchIds: [], - selectablePatches: [], - defaultPatchId: null, - }; - } + if (error) throw error; const savedPatchIds = rows.map((row) => row.patch_id); let selectablePatches: SelectablePatch[] = rows.map((row) => row.patches).flat().map((patch) => ({ id: patch.id, @@ -36,14 +33,7 @@ export async function getPatcherSelectablePatches( .select("id, version, created_at, published, archived, filename") .eq("id", currentPatchId) .maybeSingle(); - if (currentPatchError) { - console.error(currentPatchError); - return { - savedPatchIds: [], - selectablePatches: [], - defaultPatchId: null, - }; - } + if (currentPatchError) throw currentPatchError; if (currentPatch?.published && !currentPatch?.archived) { selectablePatches = [currentPatch]; }