-
Notifications
You must be signed in to change notification settings - Fork 4k
Add a force-revalidate api route #3263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
--- | ||
"gitbook-v2": patch | ||
--- | ||
|
||
add a force-revalidate api route to force bust the cache in case of errors |
This file contains hidden or 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,5 +1,6 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/image-types/global" /> | ||
/// <reference types="next/navigation-types/compat/navigation" /> | ||
|
||
// NOTE: This file should not be edited | ||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information. |
49 changes: 49 additions & 0 deletions
49
packages/gitbook-v2/src/pages/api/~gitbook/force-revalidate.ts
This file contains hidden or 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,49 @@ | ||
import crypto from 'node:crypto'; | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
interface JsonBody { | ||
// The paths need to be the rewritten one, `res.revalidate` call don't go through the middleware | ||
paths: string[]; | ||
conico974 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
// Only allow POST requests | ||
if (req.method !== 'POST') { | ||
return res.status(405).json({ error: 'Method not allowed' }); | ||
} | ||
const signatureHeader = req.headers['x-gitbook-signature'] as string | undefined; | ||
if (!signatureHeader) { | ||
return res.status(400).json({ error: 'Missing signature header' }); | ||
} | ||
// We cannot use env from `@/v2/lib/env` here as it make it crash because of the import "server-only" in the file. | ||
if (process.env.GITBOOK_SECRET) { | ||
try { | ||
const computedSignature = crypto | ||
.createHmac('sha256', process.env.GITBOOK_SECRET) | ||
.update(JSON.stringify(req.body)) | ||
.digest('hex'); | ||
|
||
if (computedSignature === signatureHeader) { | ||
const results = await Promise.allSettled( | ||
(req.body as JsonBody).paths.map((path) => { | ||
// biome-ignore lint/suspicious/noConsole: we want to log here | ||
console.log(`Revalidating path: ${path}`); | ||
return res.revalidate(path); | ||
}) | ||
); | ||
return res.status(200).json({ | ||
success: results.every((result) => result.status === 'fulfilled'), | ||
errors: results | ||
.filter((result) => result.status === 'rejected') | ||
.map((result) => (result as PromiseRejectedResult).reason), | ||
}); | ||
} | ||
return res.status(401).json({ error: 'Invalid signature' }); | ||
} catch (error) { | ||
console.error('Error during revalidation:', error); | ||
return res.status(400).json({ error: 'Invalid request or unable to parse JSON' }); | ||
} | ||
} | ||
// If no secret is set, we do not allow revalidation | ||
return res.status(403).json({ error: 'Revalidation is disabled' }); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.