Open source CapCut alternative.
TypeScript
feat(site): blog revalidate webhook for the internal su app
David Le576ffb6f5e6d
diff --git a/site/.env.example b/site/.env.exampleindex fe19cc95..ca04d44a 100644--- a/site/.env.example+++ b/site/.env.example@@ -10,6 +10,11 @@ CHATGPT_OIDC_PRIVATE_KEY="replace-with-private-key-pem"# Vercel sends this as a bearer token on cron invocations; the cron routes# (analytics run, cut-cloud gc) reject requests without it.CRON_SECRET="replace-with-a-long-random-secret"++# Bearer token the internal su app sends to /api/blog/revalidate after a blog+# write, so this app clears and re-renders its cached blog pages. The su+# deployment holds the same value.+BLOG_REVALIDATE_SECRET=GOOGLE_CLIENT_ID="replace-with-google-web-client-id"GOOGLE_CLIENT_SECRET="replace-with-google-web-client-secret"diff --git a/site/src/app/api/blog/revalidate/route.ts b/site/src/app/api/blog/revalidate/route.tsnew file mode 100644index 00000000..f66bf0b0--- /dev/null+++ b/site/src/app/api/blog/revalidate/route.ts@@ -0,0 +1,46 @@+import { timingSafeEqual } from "node:crypto";++import { NextResponse } from "next/server";+import { z } from "zod";++import { revalidateBlogLater, revalidateBlogNow } from "@/lib/blog/revalidate";++// The blog editor runs in the internal su app, a separate deployment. Its+// writes change rows this app's blog pages have cached, and a cache clears only+// from inside the app that holds it, so the editor posts here after each write.+// Deliberately not wrapped in withDonkeyAuth — the caller is a machine, not a+// session — and gated by a shared secret, like the job worker's callback.++// Publishing waits for the pages to render again, twelve tries apart.+export const maxDuration = 60;++const bodySchema = z.object({+ slugs: z.array(z.string().trim().min(1)),+ wait: z.boolean(),+});++/** Constant-time bearer check: a byte-by-byte `!==` would leak the secret's+ * length and prefix through timing. */+function authorized(header: string, secret: string): boolean {+ const expected = Buffer.from(`Bearer ${secret}`);+ const got = Buffer.from(header);+ return got.length === expected.length && timingSafeEqual(got, expected);+}++export async function POST(request: Request) {+ const secret = process.env.BLOG_REVALIDATE_SECRET;+ if (!secret) {+ return new Response("BLOG_REVALIDATE_SECRET is not set", { status: 500 });+ }+ const authz = request.headers.get("authorization") ?? "";+ if (!authorized(authz, secret)) {+ return new Response("Unauthorized", { status: 401 });+ }+ const body = bodySchema.safeParse(await request.json().catch(() => null));+ if (!body.success) {+ return new Response("Bad request", { status: 400 });+ }+ if (body.data.wait) await revalidateBlogNow(body.data.slugs);+ else revalidateBlogLater(body.data.slugs);+ return NextResponse.json({ ok: true });+}