javascriptnext.jswebhookssanity

Issue with revalidating Next.js and Sanity documents using a webhook


I'm working on a Next.js 13 project that uses Sanity as a CMS and is deployed on Vercel. I have set up a webhook to trigger revalidation of components and pages whenever a new testimonial is added, updated, or deleted in Sanity. However, I'm encountering a TypeError that says "resolver is not a function," and I'm not sure how to resolve it.

Here's the code I have 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 });
    }
};

And here's the error message I'm receiving:

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 might be related to how I'm using the res.revalidate function or possibly an issue with my imports. Can anyone provide guidance on how to properly set up the webhook to trigger revalidation upon Sanity updates?

Additional Information:

Update:

I've made some progress on the issue. It turns out the initial "resolver is not a function" error was due to a missing export for the function. However, I've encountered a new problem. When I make a request to the API, I consistently receive a "401 Unauthorized" response with the following message:

{
  "message": "Invalid request"
}

I've attempted defining the signature and secret token directly in the code, but I'm still encountering the same result. It seems like the webhook is failing to validate the request properly.


Solution

  • I think you are messing with the signature validation. Also make sure that you Enter the validation secret in your Sanity webhook and also in your envireonment variables.

    I ill share my code, here is how i did it, though i used the app router

    import { revalidateTag } from "next/cache";
    import { type NextRequest, NextResponse } from "next/server";
    import { parseBody } from "next-sanity/webhook";
    
    
    export async function POST(req: NextRequest) {
      try {
        const { isValidSignature, body } = await parseBody<{ _type: string }>(
          req,
          process.env.SANITY_REVALIDATE_SECRET
        );
    
        if (!isValidSignature) {
          const message = "Invalid signature";
          return new Response(JSON.stringify({ message, isValidSignature, body }), {
            status: 401,
          });
        }
    
        if (!body?._type) {
          revalidateTag("page");
        } else {
          revalidateTag(body?._type);
        }
    
        return NextResponse.json({ body });
    
      } catch (err) {
        console.error(err);
        return new Response(JSON.stringify(err), { status: 500 });
      }
    }