-
-
Save bretmorris/5941e50f84427c3120496ada440eaddd to your computer and use it in GitHub Desktop.
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 { useRouter } from 'next/router' | |
import ErrorPage from 'next/error' | |
import Layout from '../components/layout' | |
import Slices from '../components/slices' | |
import { getAllPagesWithSlug, getPageBySlug } from '../lib/api' | |
export default function Page({ page, preview }) { | |
const router = useRouter() | |
if (!router.isFallback && !page?._meta?.id) { | |
return <ErrorPage statusCode={404} /> | |
} | |
const metadata = { | |
meta_title: page?.meta_title, | |
meta_description: page?.meta_description, | |
meta_image: page?.meta_image, | |
} | |
return ( | |
<Layout preview={preview} metadata={metadata}> | |
{router.isFallback ? <div>Loading…</div> : <Slices slices={page?.body} />} | |
</Layout> | |
) | |
} | |
export async function getStaticProps({ params, preview = false, previewData }) { | |
const slug = params.slug?.length ? `/${params.slug.join('/')}/` : '/' | |
const data = await getPageBySlug(slug, previewData) | |
return { | |
props: { | |
preview, | |
page: data?.node ?? null, | |
}, | |
revalidate: 1, | |
} | |
} | |
export async function getStaticPaths() { | |
const allPages = await getAllPagesWithSlug() | |
const paths = allPages | |
?.filter(({ node }) => node.slug !== '/') | |
.map(({ node }) => ({ | |
params: { slug: node.slug.split('/').filter((path) => path !== '') }, | |
})) | |
return { | |
paths, | |
fallback: true, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment