-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate static pages in next recipe
- Loading branch information
Showing
10 changed files
with
97 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,9 @@ | ||
"use client"; | ||
|
||
import type { Data } from "@measured/puck"; | ||
import { Puck, Render } from "@measured/puck"; | ||
import { Render } from "@measured/puck"; | ||
import config from "../../puck.config"; | ||
|
||
export function Client({ | ||
path, | ||
data, | ||
isEdit, | ||
}: { | ||
path: string; | ||
data: Data; | ||
isEdit: boolean; | ||
}) { | ||
if (isEdit) { | ||
return ( | ||
<Puck | ||
config={config} | ||
data={data} | ||
onPublish={async (data: Data) => { | ||
await fetch("/api/puck", { | ||
method: "post", | ||
body: JSON.stringify({ data, path }), | ||
}); | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
export function Client({ data }: { data: Data }) { | ||
return <Render config={config} data={data} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,33 @@ | ||
import { Client } from "./client"; | ||
import { notFound } from "next/navigation"; | ||
import resolvePuckPath from "./resolve-puck-path"; | ||
import { Metadata } from "next"; | ||
import { Data } from "@measured/puck"; | ||
import fs from "fs"; | ||
|
||
// Replace with call to your database | ||
const getPage = (path: string) => { | ||
const allData: Record<string, Data> | null = fs.existsSync("database.json") | ||
? JSON.parse(fs.readFileSync("database.json", "utf-8")) | ||
: null; | ||
|
||
return allData ? allData[path] : null; | ||
}; | ||
import { getPage } from "../../lib/get-page"; | ||
|
||
export async function generateMetadata({ | ||
params, | ||
params: { puckPath = [] }, | ||
}: { | ||
params: { puckPath: string[] }; | ||
}): Promise<Metadata> { | ||
const { isEdit, path } = resolvePuckPath(params.puckPath); | ||
|
||
if (isEdit) { | ||
return { | ||
title: "Puck: " + path, | ||
}; | ||
} | ||
const path = `/${puckPath.join("/")}`; | ||
|
||
return { | ||
title: getPage(path)?.root.title, | ||
}; | ||
} | ||
|
||
export default async function Page({ | ||
params, | ||
params: { puckPath = [] }, | ||
}: { | ||
params: { puckPath: string[] }; | ||
}) { | ||
const { isEdit, path } = resolvePuckPath(params.puckPath); | ||
|
||
const path = `/${puckPath.join("/")}`; | ||
const data = getPage(path); | ||
|
||
if (!data && !isEdit) { | ||
if (!data) { | ||
return notFound(); | ||
} | ||
|
||
return <Client isEdit={isEdit} path={path} data={data} />; | ||
return <Client data={data} />; | ||
} | ||
|
||
export const dynamic = "force-static"; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"use client"; | ||
|
||
import type { Data } from "@measured/puck"; | ||
import { Puck } from "@measured/puck"; | ||
import config from "../../../puck.config"; | ||
|
||
export function Client({ path, data }: { path: string; data: Data }) { | ||
return ( | ||
<Puck | ||
config={config} | ||
data={data} | ||
onPublish={async (data: Data) => { | ||
await fetch("/puck/api", { | ||
method: "post", | ||
body: JSON.stringify({ data, path }), | ||
}); | ||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Client } from "./client"; | ||
import { Metadata } from "next"; | ||
import { getPage } from "../../../lib/get-page"; | ||
|
||
export async function generateMetadata({ | ||
params: { puckPath = [] }, | ||
}: { | ||
params: { puckPath: string[] }; | ||
}): Promise<Metadata> { | ||
const path = `/${puckPath.join("/")}`; | ||
|
||
return { | ||
title: "Puck: " + path, | ||
}; | ||
} | ||
|
||
export default async function Page({ | ||
params: { puckPath = [] }, | ||
}: { | ||
params: { puckPath: string[] }; | ||
}) { | ||
const path = `/${puckPath.join("/")}`; | ||
const data = getPage(path); | ||
|
||
return <Client path={path} data={data} />; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, generateMetadata } from "./[...puckPath]/page"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Data } from "@measured/puck"; | ||
import fs from "fs"; | ||
|
||
// Replace with call to your database | ||
export const getPage = (path: string) => { | ||
const allData: Record<string, Data> | null = fs.existsSync("database.json") | ||
? JSON.parse(fs.readFileSync("database.json", "utf-8")) | ||
: null; | ||
|
||
return allData ? allData[path] : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
import type { NextRequest } from "next/server"; | ||
|
||
export async function middleware(req: NextRequest) { | ||
const res = NextResponse.next(); | ||
|
||
if (req.method === "GET") { | ||
// Rewrite routes that match "/[...puckPath]/edit" to "/puck/[...puckPath]" | ||
if (req.nextUrl.pathname.endsWith("/edit")) { | ||
const pathWithoutEdit = req.nextUrl.pathname.slice( | ||
0, | ||
req.nextUrl.pathname.length - 5 | ||
); | ||
const pathWithEditPrefix = `/puck${pathWithoutEdit}`; | ||
|
||
return NextResponse.rewrite(new URL(pathWithEditPrefix, req.url)); | ||
} | ||
|
||
// Disable "/puck/[...puckPath]" | ||
if (req.nextUrl.pathname.startsWith("/puck")) { | ||
return NextResponse.redirect(new URL("/", req.url)); | ||
} | ||
} | ||
|
||
return res; | ||
} |