I'm currently developing a Next.js 13 project that utilizes Sanity as its CMS and is deployed on Vercel. To ensure that components and pages are revalidated whenever a new testimonial is added, updated, or removed in Sanity, I have configured a webhook. However, I am facing a TypeError stating "resolver is not a function," and I am uncertain how to resolve this issue.
Below is the code snippet for the webhook:
import { SIGNATURE_HEADER_NAME, isValidSignature } from "@sanity/webhook";
const handler = async (req: any, res: any) => {
try {
const signature = req.headers[SIGNATURE_HEADER_NAME].toString();
if (
!isValidSignature(
JSON.stringify(req.body),
signature,
process.env.NEXT_PUBLIC_SECRET_TOKEN as string
)
) {
return res.status(401).json({ message: "Invalid request" });
}
await res.revalidate("/");
res.status(200).json({ revalidated: true });
console.log("testimonial revalidated");
} catch (error) {
res.status(500).json({ revalidated: false, error });
}
};
The error message I am encountering reads:
TypeError: resolver is not a function
at /var/task/node_modules/next/dist/server/api-utils/node.js:463:16
// ...
I suspect that the error could be related to the usage of the res.revalidate function or potentially an issue with my imports. Can someone offer guidance on correctly configuring the webhook to trigger revalidation upon updates from Sanity?
Additional Details:
- I am using Next.js version 13 with the new app directory structure.
- The @sanity/webhook package is being employed for managing the webhook functionality.
- The webhook must initiate revalidation of components and pages when changes occur in Sanity documents.
- The revalidation should specifically occur without necessitating a complete project rebuild, only when there are updates in Sanity.
Update:
Progress has been made regarding the initial issue of "resolver is not a function," which was attributed to a missing export for the function. Nevertheless, a new challenge has emerged. Upon sending a request to the API, consistently receiving a "401 Unauthorized" response along with the following message:
{
"message": "Invalid request"
}
Despite trying different approaches such as defining the signature and secret token within the code directly, the same outcome persists. It appears that the webhook is struggling to properly authenticate the requests.