使用NextJS中的revalidateTag与Prisma在App Router和Server Components中

huangapple go评论84阅读模式
英文:

Using NextJS revalidateTag with Prisma in App Router and Server Components

问题

在Next.js 13中,使用App Router,可以在不进行fetch调用的情况下使用revalidatePath吗?我正在尝试避免使用router.refresh()进行完全刷新,而是只在特定标签上进行刷新。我了解这在使用fetch时是可能的,但由于使用了服务器组件,这种情况下不需要API路由。

例如:

import { prisma } from "@/db";

export default async function Page() {
  const addUser = async () => {
    "use server";
    await prisma.user.create({
      data: {
        email: faker.internet.email()
      }
    });
    return {
      revalidateTag: ["db"] <<<<<<---------
    }
  };

  return (
    <>
      <form action={addUser}>
        <input
          ...
        />
      </form>

      <div>{...最新的用户数据}</div>
    </>
  );
}
英文:

Is it possible in Next 13 with the App Router, to use revalidatePath without making a fetch call? I'm trying to avoid a full refresh with router.refresh() and rather just refresh on specific tags. I understand this is possible with fetch, but due to using server components an API route isn't needed in this use case.

For example:

import { prisma } from &quot;@/db&quot;;

export default async function Page() {
  const addUser = async() =&gt; {
    &quot;use server&quot;;
    await prisma.user.create({
      data: {
        email: faker.internet.email()
      }
    });
    return {
      revalidateTag: [&quot;db&quot;] &lt;&lt;&lt;&lt;&lt;---------
    }
  };

  return(
    &lt;&gt;
      &lt;form action={ addUser }&gt;
        &lt;input
          ...
        /&gt;
      &lt;/form&gt;

      &lt;div&gt;{ ...up to date user data }&lt;/div&gt;
    &lt;/&gt;
  );
}

答案1

得分: 2

我遇到了相同的问题。我找到的最佳方法是在服务器操作完成后使用 revalidatePath。

import { prisma } from "@/db";
import { revalidatePath } from 'next/cache';

export default async function Page() {
  const addUser = async () => {
    "use server";
    await prisma.user.create({
      data: {
        email: faker.internet.email()
      }
    });
         
    revalidatePath("/") //无论从哪个路径调用它
  };

  return (
    <>
      <form action={addUser}>
        <input
          ...
        />
      </form>

      <div>{...最新用户数据}</div>
    </>
  );
}

您可以在此处了解更多信息:https://nextjs.org/docs/app/api-reference/functions/revalidatePath

英文:

I'm having the same issue. The best approach I could find is to use revalidatePath inside the server action after everything is done.

import { prisma } from &quot;@/db&quot;;
import { revalidatePath } from &#39;next/cache&#39;

export default async function Page() {
  const addUser = async() =&gt; {
    &quot;use server&quot;;
    await prisma.user.create({
      data: {
        email: faker.internet.email()
      }
    });
         
    revalidatePath(&quot;/&quot;) //whatever path you are calling it from
  };

  return(
    &lt;&gt;
      &lt;form action={ addUser }&gt;
        &lt;input
          ...
        /&gt;
      &lt;/form&gt;

      &lt;div&gt;{ ...up to date user data }&lt;/div&gt;
    &lt;/&gt;
  );
}

You can learn more about it here: https://nextjs.org/docs/app/api-reference/functions/revalidatePath

huangapple
  • 本文由 发表于 2023年7月17日 20:55:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定