| 1 | import { |
| 2 | GetStaticPathsResult, |
| 3 | GetStaticPropsContext, |
| 4 | GetStaticPropsResult, |
| 5 | } from "next"; |
| 6 | |
| 7 | import { |
| 8 | Page as MakeswiftPage, |
| 9 | PageProps as MakeswiftPageProps, |
| 10 | Makeswift, |
| 11 | type SiteVersion |
| 12 | } from "@makeswift/runtime/next"; |
| 13 | |
| 14 | import { client } from "../../../../../makeswift/client"; |
| 15 | import "../../../../../makeswift/components"; |
| 16 | |
| 17 | type ParsedUrlQuery = { path?: string[] }; |
| 18 | |
| 19 | export async function getStaticPaths(): Promise< |
| 20 | GetStaticPathsResult<ParsedUrlQuery> |
| 21 | > { |
| 22 | const pages = await client.getPages().toArray(); |
| 23 | |
| 24 | return { |
| 25 | paths: pages.map((page) => ({ |
| 26 | params: { |
| 27 | path: page.path.split("/").filter((segment) => segment !== ""), |
| 28 | }, |
| 29 | })), |
| 30 | fallback: "blocking", |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | export type PageProps = MakeswiftPageProps & { |
| 35 | siteVersion: SiteVersion | null; |
| 36 | }; |
| 37 | |
| 38 | export async function getStaticProps({ |
| 39 | params, |
| 40 | previewData, |
| 41 | }: GetStaticPropsContext<ParsedUrlQuery>): Promise< |
| 42 | GetStaticPropsResult<PageProps> |
| 43 | > { |
| 44 | const path = "/" + (params?.path ?? []).join("/"); |
| 45 | const siteVersion = Makeswift.getSiteVersion(previewData); |
| 46 | const snapshot = await client.getPageSnapshot(path, { |
| 47 | siteVersion, |
| 48 | }); |
| 49 | |
| 50 | if (snapshot == null) return { notFound: true }; |
| 51 | |
| 52 | return { |
| 53 | props: { |
| 54 | snapshot, |
| 55 | siteVersion, |
| 56 | }, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | export default function Page({ snapshot }: MakeswiftPageProps) { |
| 61 | return <MakeswiftPage snapshot={snapshot} />; |
| 62 | } |