Created
January 24, 2022 22:41
-
-
Save kuccello/7c49610fcb2ab9b3183b795a894a4a6a 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
// pages/post/[slug].tsx | |
// COPIED DIRECTLY FROM: https://keystonejs.com/docs/walkthroughs/embedded-mode-with-sqlite-nextjs | |
import { GetStaticPathsResult, GetStaticPropsContext, InferGetStaticPropsType } from 'next'; | |
import Link from 'next/link'; | |
import { query } from '.keystone/api'; | |
import { Lists } from '.keystone/types'; | |
type Post = { | |
id: string; | |
title: string; | |
content: string; | |
}; | |
export default function PostPage({ post }: { post: Post }) { | |
return ( | |
<div> | |
<main style={{ margin: '3rem' }}> | |
<div> | |
<Link href="/"> | |
<a>← back home</a> | |
</Link> | |
</div> | |
<h1>{post.title}</h1> | |
<p>{post.content}</p> | |
</main> | |
</div> | |
); | |
} | |
export async function getStaticPaths(): Promise<GetStaticPathsResult> { | |
const posts = (await query.Post.findMany({ | |
query: `slug`, | |
})) as { slug: string }[]; | |
const paths = posts | |
.filter(({ slug }) => !!slug) | |
.map(({ slug }) => `/post/${slug}`); | |
return { | |
paths, | |
fallback: false, | |
}; | |
} | |
export async function getStaticProps({ params }: GetStaticPropsContext) { | |
const post = (await query.Post.findOne({ | |
where: { slug: params!.slug as string }, | |
query: 'id title content', | |
})) as Post | null; | |
if (!post) { | |
return { notFound: true }; | |
} | |
return { props: { post } }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment