|
|
import { |
|
|
PlasmicComponent, |
|
|
ComponentRenderData, |
|
|
PlasmicRootProvider, |
|
|
extractPlasmicQueryData, |
|
|
} from "@plasmicapp/loader-nextjs"; |
|
|
import type { GetStaticPaths, GetStaticProps } from "next"; |
|
|
import Error from "next/error"; |
|
|
import { PLASMIC, PREVIEW_PLASMIC } from "../plasmic-init"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getStaticPaths: GetStaticPaths = async () => { |
|
|
const pages = await PLASMIC.fetchPages(); |
|
|
return { |
|
|
paths: pages.map((page) => ({ |
|
|
params: { catchall: page.path.substring(1).split("/") }, |
|
|
})), |
|
|
fallback: "blocking", |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getStaticProps: GetStaticProps = async (context) => { |
|
|
const { catchall } = context.params ?? {}; |
|
|
|
|
|
|
|
|
const plasmicPath = |
|
|
typeof catchall === "string" |
|
|
? catchall |
|
|
: Array.isArray(catchall) |
|
|
? `/${catchall.join("/")}` |
|
|
: "/"; |
|
|
const plasmicData = await PLASMIC.maybeFetchComponentData(plasmicPath); |
|
|
if (!plasmicData) { |
|
|
|
|
|
return { |
|
|
props: {}, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const queryCache = await extractPlasmicQueryData( |
|
|
<PlasmicRootProvider loader={PLASMIC} prefetchedData={plasmicData}> |
|
|
<PlasmicComponent component={plasmicData.entryCompMetas[0].displayName} /> |
|
|
</PlasmicRootProvider>, |
|
|
); |
|
|
|
|
|
|
|
|
return { |
|
|
props: { plasmicData, queryCache, preview: context.preview ?? null }, |
|
|
|
|
|
|
|
|
|
|
|
revalidate: 300, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function CatchallPage(props: { |
|
|
plasmicData?: ComponentRenderData; |
|
|
queryCache?: Record<string, any>; |
|
|
preview?: boolean; |
|
|
}) { |
|
|
const { plasmicData, queryCache, preview } = props; |
|
|
if (!plasmicData || plasmicData.entryCompMetas.length === 0) { |
|
|
return <Error statusCode={404} />; |
|
|
} |
|
|
const pageMeta = plasmicData.entryCompMetas[0]; |
|
|
return ( |
|
|
|
|
|
<PlasmicRootProvider |
|
|
loader={preview ? PREVIEW_PLASMIC : PLASMIC} |
|
|
prefetchedData={preview ? undefined : plasmicData} |
|
|
prefetchedQueryData={preview ? undefined : queryCache} |
|
|
> |
|
|
{ |
|
|
// plasmicData.entryCompMetas[0].displayName contains the name |
|
|
// of the component you fetched. |
|
|
} |
|
|
<PlasmicComponent component={pageMeta.displayName} /> |
|
|
</PlasmicRootProvider> |
|
|
); |
|
|
} |
|
|
|