Next.js 캐시 무효화

Hermaeus Mora ·

revalidatePath api를 사용하여 캐시를 무효화한다.

// api/revalidate/route.ts
import { revalidatePath } from \"next/cache\";
 
export async function GET(request: any) {
      const _path = request.nextUrl.searchParams.get(\"path\");
  const path = decodeURIComponent(_path);
 
  if (!path) {
        return Response.error();
  }
 
  revalidatePath(path);
 
  return NextResponse.json({
        message: `revalidating cache for ${path} succeed`,
  });
}
 
// foo.ts
const revalidatePath = encodeURI(`/post/${postId}`);
await fetch(`/api/revalidate?path=${revalidatePath}`);