Created
March 3, 2022 23:31
-
-
Save PUPeter/cadc42e9e2f7b84e87aade51751bcafb to your computer and use it in GitHub Desktop.
Plasmic Catchall
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
import * as React from "react"; | |
import { PlasmicComponent } from "@plasmicapp/loader-nextjs"; | |
import { GetStaticPaths, GetStaticProps } from "next"; | |
import { | |
ComponentRenderData, | |
PlasmicRootProvider, | |
} from "@plasmicapp/loader-react"; | |
import Error from "next/error"; | |
import { PLASMIC } from "../plasmic-init"; | |
export default function PlasmicLoaderPage(props: { | |
plasmicData?: ComponentRenderData; | |
}) { | |
const { plasmicData } = props; | |
if (!plasmicData || plasmicData.entryCompMetas.length === 0) { | |
return <Error statusCode={404} />; | |
} | |
return ( | |
<PlasmicRootProvider | |
loader={PLASMIC} | |
prefetchedData={plasmicData} | |
> | |
<PlasmicComponent component={plasmicData.entryCompMetas[0].name} /> | |
</PlasmicRootProvider> | |
); | |
} | |
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: { plasmicData }, | |
// Use revalidate if you want incremental static regeneration | |
revalidate: 60 | |
}; | |
} | |
return { | |
// non-Plasmic catch-all | |
props: {}, | |
}; | |
} | |
export const getStaticPaths: GetStaticPaths = async () => { | |
const pageModules = await PLASMIC.fetchPages(); | |
return { | |
paths: pageModules.map((mod) => ({ | |
params: { | |
catchall: mod.path.substring(1).split("/"), | |
}, | |
})), | |
// Turn on "fallback: 'blocking'" if you would like new paths created | |
// in Plasmic to be automatically available | |
fallback: false, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment