Skip to content

Commit

Permalink
Merge pull request #743 from kduprey/release/v1.4.0
Browse files Browse the repository at this point in the history
release/v1.4.0
  • Loading branch information
github-actions[bot] authored Jun 30, 2024
2 parents f0eea40 + f8bf9d2 commit 4ac60ba
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/frontend/src/app/api/disable-draft/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { SERVER_SITE_URL } from "@kduprey/config";
import { draftMode } from "next/headers";
import { NextResponse } from "next/server";

export function GET() {
draftMode().disable();
return NextResponse.redirect(new URL("/", SERVER_SITE_URL));
}
21 changes: 21 additions & 0 deletions apps/frontend/src/app/api/draft/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// ./app/api/draft/route.ts

import { validatePreviewUrl } from "@sanity/preview-url-secret";
import { draftMode } from "next/headers";
import { redirect } from "next/navigation";
import { client, token } from "@/sanity";

const clientWithToken = client.withConfig({ token });

export async function GET(request: Request) {
const { isValid, redirectTo = "/" } = await validatePreviewUrl(
clientWithToken,
request.url,
);

if (!isValid) return new Response("Invalid secret", { status: 401 });

draftMode().enable();

redirect(redirectTo);
}
39 changes: 39 additions & 0 deletions apps/frontend/src/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { revalidateTag } from "next/cache";
import { type NextRequest, NextResponse } from "next/server";
import { parseBody } from "next-sanity/webhook";

interface WebhookPayload {
_type: string;
}

export async function POST(req: NextRequest) {
try {
const { body, isValidSignature } = await parseBody<WebhookPayload>(
req,
process.env.SANITY_REVALIDATE_SECRET,
);

if (!isValidSignature) {
const message = "Invalid signature";
return new Response(JSON.stringify({ body, isValidSignature, message }), {
status: 401,
});
}

if (!body?._type) {
const message = "Bad Request";
return new Response(JSON.stringify({ body, message }), { status: 400 });
}

// If the `_type` is `page`, then all `client.fetch` calls with
// `{next: {tags: ['page']}}` will be revalidated
revalidateTag(body._type);

return NextResponse.json({ body });
} catch (err) {
console.error(err);
return new Response(JSON.stringify({ message: "Internal Server Error" }), {
status: 500,
});
}
}

0 comments on commit 4ac60ba

Please sign in to comment.