-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #743 from kduprey/release/v1.4.0
release/v1.4.0
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |