Skip to content

Commit

Permalink
fix: bust cache in generated app on publish
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Sep 7, 2023
1 parent 8041fc1 commit 6e1c8ed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function Client({
onPublish={async (data: Data) => {
await fetch("/api/puck", {
method: "post",
body: JSON.stringify({ [path]: data }),
body: JSON.stringify({ data, path }),
});
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fs from "fs";

// Replace with call to your database
const getPage = (path: string) => {
const allData: Data | null = fs.existsSync("database.json")
const allData: Record<string, Data> | null = fs.existsSync("database.json")
? JSON.parse(fs.readFileSync("database.json", "utf-8"))
: null;

Expand All @@ -28,7 +28,7 @@ export async function generateMetadata({
}

return {
title: getPage(path),
title: getPage(path).root.title,
};
}

Expand Down
10 changes: 6 additions & 4 deletions packages/create-puck-app/templates/next/app/api/puck/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { revalidatePath } from "next/cache";
import { NextResponse } from "next/server";
import fs from "fs";

export async function POST(request: Request) {
const data = await request.json();
const payload = await request.json();

const existingData = JSON.parse(
fs.existsSync("database.json")
Expand All @@ -12,12 +13,13 @@ export async function POST(request: Request) {

const updatedData = {
...existingData,
...data,
[payload.path]: payload.data,
};

fs.writeFileSync("database.json", JSON.stringify(updatedData));

// Purge Next.js cache
revalidatePath(payload.path);

return NextResponse.json({ status: "ok" });
}

export const revalidate = 0;

0 comments on commit 6e1c8ed

Please sign in to comment.