Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Add on-demand revalidation to Contentful example (#39475)
## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [x] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Changes Added in on-demand revalidation for Contentful example. Specifically, added the revalidate api path, a new environment variable which will contain the secret, and some additions to the README. Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
- Loading branch information
Showing
8 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
CONTENTFUL_SPACE_ID= | ||
CONTENTFUL_ACCESS_TOKEN= | ||
CONTENTFUL_PREVIEW_ACCESS_TOKEN= | ||
CONTENTFUL_PREVIEW_SECRET= | ||
CONTENTFUL_PREVIEW_SECRET= | ||
CONTENTFUL_REVALIDATE_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// pages/api/revalidate.js | ||
|
||
export default async function handler(req, res) { | ||
// should be secret, custom header coming in from Contentful | ||
let inboundRevalToken = req.headers['x-vercel-reval-key'] | ||
|
||
// Check for secret to confirm this is a valid request | ||
if (!inboundRevalToken) { | ||
return res | ||
.status(401) | ||
.json({ message: 'x-vercel-reval-key header not defined' }) | ||
} else if (inboundRevalToken !== process.env.CONTENTFUL_REVALIDATE_SECRET) { | ||
return res.status(401).json({ message: 'Invalid token' }) | ||
} | ||
|
||
try { | ||
let postSlug = req.body.fields.slug['en-US'] | ||
|
||
// revalidate the individual post and the home page | ||
await res.revalidate(`/posts/${postSlug}`) | ||
await res.revalidate('/') | ||
|
||
return res.json({ revalidated: true }) | ||
} catch (err) { | ||
// If there was an error, Next.js will continue | ||
// to show the last successfully generated page | ||
return res.status(500).send('Error revalidating') | ||
} | ||
} |