Skip to content

Commit 8e98309

Browse files
committed
feat: create revalidate route and plugin
1 parent 3aa2d32 commit 8e98309

File tree

9 files changed

+587
-3
lines changed

9 files changed

+587
-3
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
WORDPRESS_URL="https://wordpress.com"
22
WORDPRESS_HOSTNAME="wordpress.com"
3+
4+
# If using the revalidate plugin
5+
WORDPRESS_WEBHOOK_SECRET="your-secret-key-here"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ yarn-error.log*
3434
# typescript
3535
*.tsbuildinfo
3636
next-env.d.ts
37+
38+
CLAUDE.md

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,15 @@ The WordPress API functions use a hierarchical cache tag system:
433433

434434
1. **Install the WordPress Plugin:**
435435

436-
- Navigate to `wordpress/next-revalidate/`
437-
- Create a zip file of the folder
436+
- Navigate to the `/plugin` directory
437+
- Use the pre-built `next-revalidate.zip` file or create a ZIP from the `next-revalidate` folder
438438
- Install and activate through WordPress admin
439439
- Go to Settings > Next.js Revalidation
440440
- Configure your Next.js URL and webhook secret
441441

442442
2. **Configure Next.js:**
443443

444-
- Add `WORDPRESS_WEBHOOK_SECRET` to your environment variables
444+
- Add `WORDPRESS_WEBHOOK_SECRET` to your environment variables (same secret as in WordPress plugin)
445445
- The webhook endpoint at `/api/revalidate` is already set up
446446
- No additional configuration needed
447447

@@ -451,6 +451,16 @@ The WordPress API functions use a hierarchical cache tag system:
451451
- Next.js automatically revalidates the appropriate cache tags
452452
- Only affected content is updated, maintaining performance
453453

454+
### Plugin Features
455+
456+
The Next.js Revalidation plugin includes:
457+
458+
- Automatic revalidation when posts, pages, categories, tags, authors, or media are modified
459+
- Settings page to configure your Next.js site URL and webhook secret
460+
- Manual revalidation option for full site refresh
461+
- Support for custom post types and taxonomies
462+
- Optional admin notifications for revalidation events
463+
454464
### Manual Revalidation
455465

456466
You can also manually revalidate content using the `revalidateWordPressData` function:

app/api/revalidate/route.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { revalidateTag } from "next/cache";
2+
import { NextRequest, NextResponse } from "next/server";
3+
4+
export async function POST(request: NextRequest) {
5+
try {
6+
const requestBody = await request.json();
7+
const secret = request.headers.get("x-webhook-secret");
8+
9+
// Validate webhook secret
10+
if (secret !== process.env.WORDPRESS_WEBHOOK_SECRET) {
11+
console.error("Invalid webhook secret");
12+
return NextResponse.json(
13+
{ message: "Invalid webhook secret" },
14+
{ status: 401 }
15+
);
16+
}
17+
18+
// Extract content type and ID from the webhook payload
19+
const { contentType, contentId } = requestBody;
20+
21+
if (!contentType) {
22+
return NextResponse.json(
23+
{ message: "Missing content type" },
24+
{ status: 400 }
25+
);
26+
}
27+
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+
}
63+
64+
// Revalidate all determined tags
65+
for (const tag of tagsToRevalidate) {
66+
console.log(`Revalidating tag: ${tag}`);
67+
revalidateTag(tag);
68+
}
69+
70+
return NextResponse.json({
71+
revalidated: true,
72+
message: `Revalidated tags: ${tagsToRevalidate.join(", ")}`,
73+
});
74+
} catch (error) {
75+
console.error("Revalidation error:", error);
76+
return NextResponse.json(
77+
{ message: "Error revalidating content" },
78+
{ status: 500 }
79+
);
80+
}
81+
}

plugin/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Next.js WordPress Revalidation Plugin
2+
3+
This plugin enables automatic revalidation of your Next.js site when content is changed in WordPress.
4+
5+
## Installation
6+
7+
1. Upload the `next-revalidate.zip` file through the WordPress admin plugin installer, or
8+
2. Extract the `next-revalidate` folder to your `/wp-content/plugins/` directory
9+
3. Activate the plugin through the WordPress admin interface
10+
4. Go to Settings > Next.js Revalidation to configure your settings
11+
12+
## Configuration
13+
14+
### 1. WordPress Plugin Settings
15+
16+
After installing and activating the plugin:
17+
18+
1. Go to Settings > Next.js Revalidation in your WordPress admin
19+
2. Enter your Next.js site URL (without trailing slash)
20+
3. Create a secure webhook secret (a random string)
21+
4. Save your settings
22+
23+
### 2. Next.js Environment Variables
24+
25+
Add the webhook secret to your Next.js environment variables:
26+
27+
```bash
28+
# .env.local
29+
WORDPRESS_WEBHOOK_SECRET="your-secret-key-here"
30+
```
31+
32+
## How It Works
33+
34+
1. When content in WordPress is created, updated, or deleted, the plugin sends a webhook to your Next.js API route
35+
2. The webhook contains information about the content type (post, page, category, etc.) and ID
36+
3. The Next.js API validates the request using the secret and revalidates the appropriate cache tags
37+
4. Your Next.js site will fetch new content for the affected pages
38+
39+
## Features
40+
41+
- Automatic revalidation for posts, pages, categories, tags, and media
42+
- Manual revalidation option through the admin interface
43+
- Secure webhook communication with your Next.js site
44+
- Optional admin notifications for revalidation events
45+
46+
## Troubleshooting
47+
48+
If revalidation isn't working:
49+
50+
1. Check that your Next.js URL is correct in the plugin settings
51+
2. Verify the webhook secret matches in both WordPress and Next.js
52+
3. Check your server logs for any errors in the API route
53+
4. Enable notifications in the plugin settings to see revalidation status

plugin/next-revalidate.zip

4.96 KB
Binary file not shown.

plugin/next-revalidate/README.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
=== Next.js Revalidation ===
2+
Contributors: 9d8
3+
Tags: next.js, headless, revalidation, cache
4+
Requires at least: 5.0
5+
Tested up to: 6.4
6+
Stable tag: 1.0.0
7+
Requires PHP: 7.2
8+
License: GPLv2 or later
9+
License URI: https://www.gnu.org/licenses/gpl-2.0.html
10+
11+
Automatically revalidate your Next.js site when WordPress content changes.
12+
13+
== Description ==
14+
15+
Next.js Revalidation is a WordPress plugin designed to work with the `next-wp` Next.js starter template. It triggers revalidation of your Next.js site's cache whenever content is added, updated, or deleted in WordPress.
16+
17+
The plugin sends webhooks to your Next.js site's revalidation API endpoint, ensuring your headless frontend always displays the most up-to-date content.
18+
19+
**Key Features:**
20+
21+
* Automatic revalidation when posts, pages, categories, tags, authors, or media are modified
22+
* Settings page to configure your Next.js site URL and webhook secret
23+
* Manual revalidation option for full site refresh
24+
* Support for custom post types and taxonomies
25+
* Optional admin notifications for revalidation events
26+
27+
== Installation ==
28+
29+
1. Upload the `next-revalidate` folder to the `/wp-content/plugins/` directory
30+
2. Activate the plugin through the 'Plugins' menu in WordPress
31+
3. Go to Settings > Next.js Revalidation to configure your Next.js site URL and webhook secret
32+
33+
== Configuration ==
34+
35+
1. Visit Settings > Next.js Revalidation in your WordPress admin
36+
2. Enter your Next.js site URL without a trailing slash (e.g., https://your-site.com)
37+
3. Enter the webhook secret which should match the WORDPRESS_WEBHOOK_SECRET in your Next.js environment
38+
4. Optionally enable admin notifications for revalidation events
39+
5. Click "Save Settings"
40+
41+
== Frequently Asked Questions ==
42+
43+
= What is the webhook secret for? =
44+
45+
The webhook secret provides security for your revalidation API endpoint. It ensures that only your WordPress site can trigger revalidations.
46+
47+
= How do I set up my Next.js site for revalidation? =
48+
49+
Your Next.js site needs an API endpoint at `/api/revalidate` that can process the webhook payloads from this plugin.
50+
See the README in your Next.js project for more details.
51+
52+
= Does this work with custom post types? =
53+
54+
Yes, the plugin automatically detects and handles revalidation for custom post types and taxonomies.
55+
56+
== Changelog ==
57+
58+
= 1.0.0 =
59+
* Initial release
60+
61+
== Upgrade Notice ==
62+
63+
= 1.0.0 =
64+
Initial release of the Next.js Revalidation plugin.

plugin/next-revalidate/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
// Silence is golden.

0 commit comments

Comments
 (0)