1
- import { revalidateTag } from "next/cache" ;
1
+ import { revalidatePath } from "next/cache" ;
2
2
import { NextRequest , NextResponse } from "next/server" ;
3
3
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
+
4
12
export async function POST ( request : NextRequest ) {
5
13
try {
6
14
const requestBody = await request . json ( ) ;
7
15
const secret = request . headers . get ( "x-webhook-secret" ) ;
8
16
9
- // Validate webhook secret
10
17
if ( secret !== process . env . WORDPRESS_WEBHOOK_SECRET ) {
11
18
console . error ( "Invalid webhook secret" ) ;
12
19
return NextResponse . json (
@@ -15,7 +22,6 @@ export async function POST(request: NextRequest) {
15
22
) ;
16
23
}
17
24
18
- // Extract content type and ID from the webhook payload
19
25
const { contentType, contentId } = requestBody ;
20
26
21
27
if ( ! contentType ) {
@@ -25,57 +31,38 @@ export async function POST(request: NextRequest) {
25
31
) ;
26
32
}
27
33
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" ) ;
63
37
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
+ ) ;
68
56
}
69
-
70
- return NextResponse . json ( {
71
- revalidated : true ,
72
- message : `Revalidated tags: ${ tagsToRevalidate . join ( ", " ) } ` ,
73
- } ) ;
74
57
} catch ( error ) {
75
58
console . error ( "Revalidation error:" , error ) ;
76
59
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
+ } ,
78
65
{ status : 500 }
79
66
) ;
80
67
}
81
- }
68
+ }
0 commit comments