Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 9567b94

Browse files
authored
Merge branch 'main' into patch-2
2 parents 2016ebb + adf7874 commit 9567b94

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Code Owners
22
# https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners
3-
* @gregrickaby
3+

pages/api/wordpress/revalidate.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* On-demand post revalidation.
3+
*
4+
* @see https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration#using-on-demand-revalidation
5+
* @author WebDevStudios
6+
* @param {object} req Instance of http.IncomingMessage.
7+
* @param {object} res Instance of http.ServerResponse.
8+
*/
9+
export default async function revalidate(req, res) {
10+
// Check for a valid secret.
11+
if (req.body.secret !== process.env.WORDPRESS_PREVIEW_SECRET) {
12+
return res.status(401).json({
13+
message:
14+
'Invalid secret. Please check your .env file or the POST request.'
15+
})
16+
}
17+
18+
// Check for a valid post slug.
19+
if (!req.body.slug) {
20+
return res.status(400).json({
21+
message: 'A slug is required to revalidate the cache.'
22+
})
23+
}
24+
25+
// Try to revalidate the post cache.
26+
try {
27+
await res.unstable_revalidate(req.body.slug)
28+
return res.status(200).json({
29+
message: `Success! The cache for ${req.body.slug} was successfully revalidated.`
30+
})
31+
} catch (err) {
32+
return res.status(500).json({
33+
message: err.message
34+
})
35+
}
36+
}

0 commit comments

Comments
 (0)