create-puck-app 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-puck-app",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -21,7 +21,7 @@ export function Client({
21
21
  onPublish={async (data: Data) => {
22
22
  await fetch("/api/puck", {
23
23
  method: "post",
24
- body: JSON.stringify({ [path]: data }),
24
+ body: JSON.stringify({ data, path }),
25
25
  });
26
26
  }}
27
27
  />
@@ -2,7 +2,17 @@ import { Client } from "./client";
2
2
  import { notFound } from "next/navigation";
3
3
  import resolvePuckPath from "./resolve-puck-path";
4
4
  import { Metadata } from "next";
5
- import { Data } from "@measured/puck/types/Config";
5
+ import { Data } from "@measured/puck";
6
+ import fs from "fs";
7
+
8
+ // Replace with call to your database
9
+ const getPage = (path: string) => {
10
+ const allData: Record<string, Data> | null = fs.existsSync("database.json")
11
+ ? JSON.parse(fs.readFileSync("database.json", "utf-8"))
12
+ : null;
13
+
14
+ return allData ? allData[path] : null;
15
+ };
6
16
 
7
17
  export async function generateMetadata({
8
18
  params,
@@ -17,14 +27,8 @@ export async function generateMetadata({
17
27
  };
18
28
  }
19
29
 
20
- const data: Data = (
21
- await fetch("http://localhost:3000/api/puck", {
22
- next: { revalidate: 0 },
23
- }).then((d) => d.json())
24
- )[path];
25
-
26
30
  return {
27
- title: data?.page?.title,
31
+ title: getPage(path).root.title,
28
32
  };
29
33
  }
30
34
 
@@ -35,11 +39,7 @@ export default async function Page({
35
39
  }) {
36
40
  const { isEdit, path } = resolvePuckPath(params.puckPath);
37
41
 
38
- const data = (
39
- await fetch("http://localhost:3000/api/puck", {
40
- next: { revalidate: 0 },
41
- }).then((d) => d.json())
42
- )[path];
42
+ const data = getPage(path);
43
43
 
44
44
  if (!data && !isEdit) {
45
45
  return notFound();
@@ -1,16 +1,9 @@
1
+ import { revalidatePath } from "next/cache";
1
2
  import { NextResponse } from "next/server";
2
3
  import fs from "fs";
3
4
 
4
- export async function GET() {
5
- const data = fs.existsSync("database.json")
6
- ? fs.readFileSync("database.json", "utf-8")
7
- : null;
8
-
9
- return NextResponse.json(JSON.parse(data || "{}"));
10
- }
11
-
12
5
  export async function POST(request: Request) {
13
- const data = await request.json();
6
+ const payload = await request.json();
14
7
 
15
8
  const existingData = JSON.parse(
16
9
  fs.existsSync("database.json")
@@ -20,12 +13,13 @@ export async function POST(request: Request) {
20
13
 
21
14
  const updatedData = {
22
15
  ...existingData,
23
- ...data,
16
+ [payload.path]: payload.data,
24
17
  };
25
18
 
26
19
  fs.writeFileSync("database.json", JSON.stringify(updatedData));
27
20
 
21
+ // Purge Next.js cache
22
+ revalidatePath(payload.path);
23
+
28
24
  return NextResponse.json({ status: "ok" });
29
25
  }
30
-
31
- export const revalidate = 0;
@@ -0,0 +1 @@
1
+ {"/":{"content":[{"type":"HeadingBlock","props":{"title":"Edit this page by adding /edit to the end of the URL","id":"HeadingBlock-1694032984497"}}],"root":{"title":""}}}