按需重新验证NextJS 13 /应用程序目录?

shyt4zoc  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(177)

我正在尝试使用next 13/应用程序目录进行按需重新验证。我知道如何使用app-directory重新创建以前需要的getServerSidePropsgetStaticProps
但是我们的网站上有一个页面,每当触发器/ webhook被触发时,它就需要重建(该页面)。我知道我可以

export const revalidate = XXXX;

...but it's not the same, I don't need to rebuild the site every X seconds.

Any ideas?

tzdcorbm

tzdcorbm1#

当我们使用nextjs静态构建时,我们需要重新验证,它是在getStaticProps和getStaticPaths的帮助下生成的。我们所有的网页将建立为html文件。我们也可以将我们的应用程序作为一个混合模型,一些页面将静态生成,一些页面将在服务器端呈现。
对于重新验证,首先需要在/pages/api目录中创建一个重新验证API。

exec("npm run build", (error, stdout, stderr) => {
if (error) {
  console.error(`Error during build: ${error.message}`);
  return res.status(500).json({
    message: "Failed to trigger revalidation",
    data: error.message,
    code: 422,
  });
}
return res
  .status(200)
  .json({ message: "Published Successfully", code: 200 });

在子进程的帮助下,您可以触发一个构建,该构建将重新生成所有具有新数据的页面,这些数据在构建时被填充。
现在为了触发这个API的重新验证调用,你也可以添加secret token来验证天气请求是否有效。

相关问题