|
| 1 | +import { Fragment } from 'react'; |
| 2 | +import Head from 'next/head'; |
| 3 | +import Link from 'next/link'; |
| 4 | + |
| 5 | +import { |
| 6 | + getDatabase, getBlocks, getPageFromSlug, |
| 7 | +} from '../../../lib/notion'; |
| 8 | +import Text from '../../../components/text'; |
| 9 | +import { renderBlock } from '../../../components/notion/renderer'; |
| 10 | +import styles from '../../../styles/post.module.css'; |
| 11 | + |
| 12 | +// Return a list of `params` to populate the [slug] dynamic segment |
| 13 | +export async function generateStaticParams() { |
| 14 | + const database = await getDatabase(); |
| 15 | + return database?.map((page) => { |
| 16 | + const slug = page.properties.Slug?.formula?.string; |
| 17 | + return ({ id: page.id, slug }); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +export default async function Page({ params }) { |
| 22 | + const page = await getPageFromSlug(params?.slug); |
| 23 | + const blocks = await getBlocks(page?.id); |
| 24 | + |
| 25 | + if (!page || !blocks) { |
| 26 | + return <div />; |
| 27 | + } |
| 28 | + |
| 29 | + return ( |
| 30 | + <div> |
| 31 | + <Head> |
| 32 | + <title>{page.properties.Title?.title[0].plain_text}</title> |
| 33 | + <link rel="icon" href="/favicon.ico" /> |
| 34 | + </Head> |
| 35 | + |
| 36 | + <article className={styles.container}> |
| 37 | + <h1 className={styles.name}> |
| 38 | + <Text title={page.properties.Title?.title} /> |
| 39 | + </h1> |
| 40 | + <section> |
| 41 | + {blocks.map((block) => ( |
| 42 | + <Fragment key={block.id}>{renderBlock(block)}</Fragment> |
| 43 | + ))} |
| 44 | + <Link href="/" className={styles.back}> |
| 45 | + ← Go home |
| 46 | + </Link> |
| 47 | + </section> |
| 48 | + </article> |
| 49 | + </div> |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +// export const getStaticPaths = async () => { |
| 54 | +// const database = await getDatabase(databaseId); |
| 55 | +// return { |
| 56 | +// paths: database.map((page) => { |
| 57 | +// const slug = page.properties.Slug?.formula?.string; |
| 58 | +// return ({ params: { id: page.id, slug } }); |
| 59 | +// }), |
| 60 | +// fallback: true, |
| 61 | +// }; |
| 62 | +// }; |
| 63 | + |
| 64 | +// export const getStaticProps = async (context) => { |
| 65 | +// const { slug } = context.params; |
| 66 | +// const page = await getPage(id); |
| 67 | +// const blocks = await getBlocks(id); |
| 68 | + |
| 69 | +// return { |
| 70 | +// props: { |
| 71 | +// page, |
| 72 | +// blocks, |
| 73 | +// }, |
| 74 | +// revalidate: 1, |
| 75 | +// }; |
| 76 | +// }; |
0 commit comments