Sanity NextJS on-demand ISR

Viewed 21

I'm currently struggling with my NextJS on-demand ISR web hook in Sanity.io. It does fire most of the time but does not actually put out a console log or new content.

This is my revalidate.ts file:

import { isValidRequest } from "@sanity/webhook";
import type { NextApiRequest, NextApiResponse } from "next";

type Data = {
  message: string;
};

const secret = process.env.SANITY_WEBHOOK_SECRET;

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

  if (!isValidRequest(req, secret!)) {
    res.status(401).json({ message: "Invalid signature" });
    return;
  }

  try {
    const {
      body: { type, slug },
    } = req;

    switch (type) {
      case "projects":
        await res.revalidate(`/`);
        return res.json({
          message: `Revalidated "${type}" with slug "${slug}"`,
        });

      case "about":
        await res.revalidate(`/info`);
        return res.json({
          message: `Revalidated "${type}" with slug "${slug}"`,
        });
    }

    return res.json({ message: "No managed type" });
  } catch (err) {
    return res.status(500).send({ message: "Error revalidating" });
  }
}

In Sanity, this is my web hook:

Filter: _type == "projects" || _type == "about"

Projection:

{
  "type":_type,
  "slug":slug.current
}

Basically, I have a one-pager with projects and when creating new projects/updating existing ones, the front-page should update. I also have an /info page that should also update when it's changed in Sanity. Help would be much appreciated!

0 Answers
Related