1- import { revalidateTag } from "next/cache" ;
1+ import { revalidatePath } from "next/cache" ;
22import { NextRequest , NextResponse } from "next/server" ;
33
4+ export const maxDuration = 30 ;
5+
6+ /**
7+ * WordPress webhook handler for content revalidation
8+ * Receives notifications from WordPress when content changes
9+ * and revalidates the entire site
10+ */
11+
412export async function POST ( request : NextRequest ) {
513 try {
614 const requestBody = await request . json ( ) ;
715 const secret = request . headers . get ( "x-webhook-secret" ) ;
816
9- // Validate webhook secret
1017 if ( secret !== process . env . WORDPRESS_WEBHOOK_SECRET ) {
1118 console . error ( "Invalid webhook secret" ) ;
1219 return NextResponse . json (
@@ -15,7 +22,6 @@ export async function POST(request: NextRequest) {
1522 ) ;
1623 }
1724
18- // Extract content type and ID from the webhook payload
1925 const { contentType, contentId } = requestBody ;
2026
2127 if ( ! contentType ) {
@@ -25,57 +31,38 @@ export async function POST(request: NextRequest) {
2531 ) ;
2632 }
2733
28- // Determine which tags to revalidate
29- const tagsToRevalidate = [ "wordpress" ] ;
30-
31- // Add content type specific tag
32- if ( contentType === "post" ) {
33- tagsToRevalidate . push ( "posts" ) ;
34- if ( contentId ) {
35- tagsToRevalidate . push ( `post-${ contentId } ` ) ;
36- }
37- } else if ( contentType === "page" ) {
38- tagsToRevalidate . push ( "pages" ) ;
39- if ( contentId ) {
40- tagsToRevalidate . push ( `page-${ contentId } ` ) ;
41- }
42- } else if ( contentType === "category" ) {
43- tagsToRevalidate . push ( "categories" ) ;
44- if ( contentId ) {
45- tagsToRevalidate . push ( `category-${ contentId } ` ) ;
46- }
47- } else if ( contentType === "tag" ) {
48- tagsToRevalidate . push ( "tags" ) ;
49- if ( contentId ) {
50- tagsToRevalidate . push ( `tag-${ contentId } ` ) ;
51- }
52- } else if ( contentType === "author" || contentType === "user" ) {
53- tagsToRevalidate . push ( "authors" ) ;
54- if ( contentId ) {
55- tagsToRevalidate . push ( `author-${ contentId } ` ) ;
56- }
57- } else if ( contentType === "media" ) {
58- tagsToRevalidate . push ( "media" ) ;
59- if ( contentId ) {
60- tagsToRevalidate . push ( `media-${ contentId } ` ) ;
61- }
62- }
34+ try {
35+ console . log ( "Revalidating entire site" ) ;
36+ revalidatePath ( "/" , "layout" ) ;
6337
64- // Revalidate all determined tags
65- for ( const tag of tagsToRevalidate ) {
66- console . log ( `Revalidating tag: ${ tag } ` ) ;
67- revalidateTag ( tag ) ;
38+ return NextResponse . json ( {
39+ revalidated : true ,
40+ message : `Revalidated entire site due to ${ contentType } update${
41+ contentId ? ` (ID: ${ contentId } )` : ""
42+ } `,
43+ timestamp : new Date ( ) . toISOString ( ) ,
44+ } ) ;
45+ } catch ( error ) {
46+ console . error ( "Error revalidating path:" , error ) ;
47+ return NextResponse . json (
48+ {
49+ revalidated : false ,
50+ message : "Failed to revalidate site" ,
51+ error : ( error as Error ) . message ,
52+ timestamp : new Date ( ) . toISOString ( ) ,
53+ } ,
54+ { status : 500 }
55+ ) ;
6856 }
69-
70- return NextResponse . json ( {
71- revalidated : true ,
72- message : `Revalidated tags: ${ tagsToRevalidate . join ( ", " ) } ` ,
73- } ) ;
7457 } catch ( error ) {
7558 console . error ( "Revalidation error:" , error ) ;
7659 return NextResponse . json (
77- { message : "Error revalidating content" } ,
60+ {
61+ message : "Error revalidating content" ,
62+ error : ( error as Error ) . message ,
63+ timestamp : new Date ( ) . toISOString ( ) ,
64+ } ,
7865 { status : 500 }
7966 ) ;
8067 }
81- }
68+ }
0 commit comments