0

I have two problems with NextJS, both are connected I believe.

both work fine on my local Windows machine, but not on the production server (Digital Ocean droplet Nginx + NextJS)

res.revalidate('/blog/post-1') returns Error: Invalid response 404

I believe the error is being thrown from this block of code below from node_modules/next/dist/server/api-utils/node.js

   if (context.trustHostHeader) {
            const res = await fetch(`https://${req.headers.host}${urlPath}`, {
                method: "HEAD",
                headers: revalidateHeaders
            });
            // we use the cache header to determine successful revalidate as
            // a non-200 status code can be returned from a successful revalidate
            // e.g. notFound: true returns 404 status code but is successful
            const cacheHeader = res.headers.get("x-vercel-cache") || res.headers.get("x-nextjs-cache");
            if ((cacheHeader == null ? void 0 : cacheHeader.toUpperCase()) !== "REVALIDATED" && !(res.status === 404 && opts.unstable_onlyGenerated)) {
                throw new Error(`Invalid response ${res.status}`);
            }
        } else if (context.revalidate) {

my code from page/api/revalidate.ts

import { NextApiRequest, NextApiResponse } from "next";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  // Check for secret to confirm this is a valid request
  if (req.query.secret !== process.env.REVALIDATE_TOKEN) {
    return res.status(401).json({ message: "Invalid token" });
  }

  if (!req.body) {
    return res.status(422).json({ message: "Invalid request body" });
  }
  console.log(req.body);
  const slug = req.body.slug;
  console.log(slug);

  try {
    // await res.revalidate(`/blog`);
    await res.revalidate(`/blog/${slug}`);
    console.log("Build hook");

    return res.status(200).json({ revalidated: true });
  } catch (err) {
    console.log(err);
    return res.status(500).send("Error revalidating");
  }
}

getStaticProps() also does not revalidate, even though revalidate: 60.

visting the page 'blog/new-page-slug' should fire off getStaticProps in [slug].tsx and build the new page if the slug is found in the DB. But it does not run. Yet it works fine on local machine.

0 Answers0