2020-10-29 19:03:43 -05:00
|
|
|
import type {
|
|
|
|
GetStaticPathsContext,
|
|
|
|
GetStaticPropsContext,
|
|
|
|
InferGetStaticPropsType,
|
|
|
|
} from 'next'
|
2021-06-01 03:18:10 -05:00
|
|
|
import commerce from '@lib/api/commerce'
|
2021-01-09 12:36:48 -03:00
|
|
|
import { Text } from '@components/ui'
|
|
|
|
import { Layout } from '@components/common'
|
2020-11-26 13:36:59 -03:00
|
|
|
import getSlug from '@lib/get-slug'
|
|
|
|
import { missingLocaleInPages } from '@lib/usage-warns'
|
2021-06-15 20:23:17 -03:00
|
|
|
import type { Page } from '@commerce/types/page'
|
2021-06-07 23:12:20 +03:00
|
|
|
import { useRouter } from 'next/router'
|
2020-10-15 18:00:33 -05:00
|
|
|
|
|
|
|
export async function getStaticProps({
|
|
|
|
preview,
|
|
|
|
params,
|
2020-10-25 13:31:12 -05:00
|
|
|
locale,
|
2021-06-01 03:18:10 -05:00
|
|
|
locales,
|
2020-10-15 18:00:33 -05:00
|
|
|
}: GetStaticPropsContext<{ pages: string[] }>) {
|
2021-06-01 03:18:10 -05:00
|
|
|
const config = { locale, locales }
|
2021-06-15 20:23:17 -03:00
|
|
|
const pagesPromise = commerce.getAllPages({ config, preview })
|
|
|
|
const siteInfoPromise = commerce.getSiteInfo({ config, preview })
|
|
|
|
const { pages } = await pagesPromise
|
|
|
|
const { categories } = await siteInfoPromise
|
2020-10-25 13:31:12 -05:00
|
|
|
const path = params?.pages.join('/')
|
|
|
|
const slug = locale ? `${locale}/${path}` : path
|
2021-06-15 20:23:17 -03:00
|
|
|
const pageItem = pages.find((p: Page) =>
|
|
|
|
p.url ? getSlug(p.url) === slug : false
|
|
|
|
)
|
2020-10-27 00:47:29 -05:00
|
|
|
const data =
|
2020-10-27 04:00:42 -05:00
|
|
|
pageItem &&
|
2021-06-01 03:18:10 -05:00
|
|
|
(await commerce.getPage({
|
|
|
|
variables: { id: pageItem.id! },
|
|
|
|
config,
|
|
|
|
preview,
|
|
|
|
}))
|
2021-06-07 23:12:20 +03:00
|
|
|
|
2020-10-15 18:00:33 -05:00
|
|
|
const page = data?.page
|
|
|
|
|
|
|
|
if (!page) {
|
2020-10-25 13:31:12 -05:00
|
|
|
// We throw to make sure this fails at build time as this is never expected to happen
|
2020-10-15 18:00:33 -05:00
|
|
|
throw new Error(`Page with slug '${slug}' not found`)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-05-31 19:44:08 -03:00
|
|
|
props: { pages, page, categories },
|
2020-10-25 13:31:12 -05:00
|
|
|
revalidate: 60 * 60, // Every hour
|
2020-10-15 18:00:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:03:43 -05:00
|
|
|
export async function getStaticPaths({ locales }: GetStaticPathsContext) {
|
2021-06-01 03:18:10 -05:00
|
|
|
const config = { locales }
|
2021-06-15 20:23:17 -03:00
|
|
|
const { pages }: { pages: Page[] } = await commerce.getAllPages({ config })
|
2020-10-29 19:21:46 -05:00
|
|
|
const [invalidPaths, log] = missingLocaleInPages()
|
|
|
|
const paths = pages
|
|
|
|
.map((page) => page.url)
|
|
|
|
.filter((url) => {
|
|
|
|
if (!url || !locales) return url
|
|
|
|
// If there are locales, only include the pages that include one of the available locales
|
|
|
|
if (locales.includes(getSlug(url).split('/')[0])) return url
|
2020-10-15 18:00:33 -05:00
|
|
|
|
2020-10-29 19:21:46 -05:00
|
|
|
invalidPaths.push(url)
|
|
|
|
})
|
|
|
|
log()
|
2020-10-29 19:03:43 -05:00
|
|
|
|
2020-10-29 19:21:46 -05:00
|
|
|
return {
|
|
|
|
paths,
|
2021-06-07 23:12:20 +03:00
|
|
|
fallback: 'blocking',
|
2020-10-15 18:00:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Pages({
|
|
|
|
page,
|
|
|
|
}: InferGetStaticPropsType<typeof getStaticProps>) {
|
2021-06-07 23:12:20 +03:00
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
return router.isFallback ? (
|
|
|
|
<h1>Loading...</h1> // TODO (BC) Add Skeleton Views
|
|
|
|
) : (
|
2021-01-29 12:00:16 -03:00
|
|
|
<div className="max-w-2xl mx-8 sm:mx-auto py-20">
|
2020-11-26 13:38:37 -03:00
|
|
|
{page?.body && <Text html={page.body} />}
|
2020-10-15 18:42:03 -05:00
|
|
|
</div>
|
|
|
|
)
|
2020-10-15 18:00:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Pages.Layout = Layout
|