Skip to content
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

Revalidate fix #7

Merged
merged 4 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(Carousel revalidation): Adds a new API endpoint to revalidate car…
…ousel images
  • Loading branch information
gonstoll committed Dec 28, 2022
commit 57a3312832d3e06ddada35764b6760ada11cc52d
27 changes: 27 additions & 0 deletions pages/api/revalidate-carousel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {isValidSignature, SIGNATURE_HEADER_NAME} from '@sanity/webhook';
import type {NextApiRequest, NextApiResponse} from 'next';

const secret = process.env.SANITY_WEBHOOK_SECRET!;

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== 'POST') {
return res.status(401).json({message: 'Must be a POST request'});
}

const signature = req.headers[SIGNATURE_HEADER_NAME]?.toString() || '';
if (!isValidSignature(JSON.stringify(req.body), signature, secret)) {
return res.status(401).json({message: 'Invalid signature'});
}

try {
await res.revalidate('/');
console.log('Revalidated [carousel]');
return res.json({revalidated: true});
} catch (err) {
console.log('Error revalidating [carousel]: ', err);
return res.status(500).send({message: 'Error revalidating'});
}
}
4 changes: 2 additions & 2 deletions pages/api/revalidate.ts → pages/api/revalidate-posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export default async function handler(
const pathToRevalidate = req.body.slug;
await res.revalidate(`/work/${pathToRevalidate}`);
await res.revalidate('/');
console.log('Revalidated');
console.log('Revalidated [posts]');
return res.json({revalidated: true});
} catch (err) {
console.log('Error revalidating', err);
console.log('Error revalidating [posts]: ', err);
return res.status(500).send({message: 'Error revalidating'});
}
}