Unverified Commit 49d4f8af authored by José Olórtegui's avatar José Olórtegui Committed by GitHub

docs: update webhook snippet for cache revalidation (#53585)


This PR updates cache revalidation snippets in the docs. It seems that `res` should not be there since you can't find it anywhere in the snippet so I suppose that the intent was to use `NextResponse` instead. Another thing is that now we're checking the existence of the params so typescript doesn't get mad at us. And last I changed the methods to `POST` since AFAIK webhooks normally use the `POST` method.

Please let me know if there's anything left to do on my side.

Have a great day!
Co-authored-by: 's avatarSteven <229881+styfle@users.noreply.github.com>
parent e7a3fd91
...@@ -152,16 +152,20 @@ import { NextRequest, NextResponse } from 'next/server' ...@@ -152,16 +152,20 @@ import { NextRequest, NextResponse } from 'next/server'
import { revalidateTag } from 'next/cache' import { revalidateTag } from 'next/cache'
// e.g a webhook to `your-website.com/api/revalidate?tag=collection&secret=<token>` // e.g a webhook to `your-website.com/api/revalidate?tag=collection&secret=<token>`
export async function GET(request: NextRequest) { export async function POST(request: NextRequest) {
// Check for secret to confirm this is a valid request const secret = request.nextUrl.searchParams.get('secret')
if ( const tag = request.nextUrl.searchParams.get('tag')
request.nextUrl.searchParams.get('secret') !== process.env.MY_SECRET_TOKEN
) { if (secret !== process.env.MY_SECRET_TOKEN) {
return res.status(401).json({ message: 'Invalid token' }) return NextResponse.json({ message: 'Invalid secret' }, { status: 401 })
}
if (!tag) {
return NextResponse.json({ message: 'Missing tag param' }, { status: 400 })
} }
const tag = request.nextUrl.searchParams.get('tag')
revalidateTag(tag) revalidateTag(tag)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} }
``` ```
...@@ -171,16 +175,20 @@ import { NextResponse } from 'next/server' ...@@ -171,16 +175,20 @@ import { NextResponse } from 'next/server'
import { revalidateTag } from 'next/cache' import { revalidateTag } from 'next/cache'
// e.g a webhook to `your-website.com/api/revalidate?tag=collection&secret=<token>` // e.g a webhook to `your-website.com/api/revalidate?tag=collection&secret=<token>`
export async function GET(request) { export async function POST(request) {
// Check for secret to confirm this is a valid request const secret = request.nextUrl.searchParams.get('secret')
if ( const tag = request.nextUrl.searchParams.get('tag')
request.nextUrl.searchParams.get('secret') !== process.env.MY_SECRET_TOKEN
) { if (secret !== process.env.MY_SECRET_TOKEN) {
return res.status(401).json({ message: 'Invalid token' }) return NextResponse.json({ message: 'Invalid secret' }, { status: 401 })
}
if (!tag) {
return NextResponse.json({ message: 'Missing tag param' }, { status: 400 })
} }
const tag = request.nextUrl.searchParams.get('tag')
revalidateTag(tag) revalidateTag(tag)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} }
``` ```
...@@ -191,9 +199,15 @@ Alternatively, you can use [`revalidatePath`](/docs/app/api-reference/functions/ ...@@ -191,9 +199,15 @@ Alternatively, you can use [`revalidatePath`](/docs/app/api-reference/functions/
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { revalidatePath } from 'next/cache' import { revalidatePath } from 'next/cache'
export async function GET(request: NextRequest) { export async function POST(request: NextRequest) {
const path = request.nextUrl.searchParams.get('path') const path = request.nextUrl.searchParams.get('path')
if (!path) {
return NextResponse.json({ message: 'Missing path param' }, { status: 400 })
}
revalidatePath(path) revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} }
``` ```
...@@ -202,9 +216,15 @@ export async function GET(request: NextRequest) { ...@@ -202,9 +216,15 @@ export async function GET(request: NextRequest) {
import { NextResponse } from 'next/server' import { NextResponse } from 'next/server'
import { revalidatePath } from 'next/cache' import { revalidatePath } from 'next/cache'
export async function GET(request) { export async function POST(request) {
const path = request.nextUrl.searchParams.get('path') const path = request.nextUrl.searchParams.get('path')
if (!path) {
return NextResponse.json({ message: 'Missing path param' }, { status: 400 })
}
revalidatePath(path) revalidatePath(path)
return NextResponse.json({ revalidated: true, now: Date.now() }) return NextResponse.json({ revalidated: true, now: Date.now() })
} }
``` ```
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment