Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Last active March 23, 2022 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsh-code/d9829996f48409162d4c61f8955cf0f8 to your computer and use it in GitHub Desktop.
Save tsh-code/d9829996f48409162d4c61f8955cf0f8 to your computer and use it in GitHub Desktop.
ssg.tsx
import { GetStaticPathsResult, GetStaticPropsResult, GetStaticPropsContext } from 'next';
type Post = {
id: string;
title: string;
}
type PostPageProps = {
post: Post;
}
const PostPage = ({ post }: PostPageProps) => {
return (
<p>{post.title}</p>
)
}
export async function getStaticPaths(): Promise<GetStaticPathsResult> {
const data = await fetch('http://external-api.com/posts');
const posts = await data.json();
return {
paths: posts.map(post => ({
params: { id: post.id }
})),
fallback: false,
}
}
export async function getStaticProps({ params }: GetStaticPropsContext<{ id?: string }>): Promise<GetStaticPropsResult<PostPageProps>> {
const data = await fetch(`http://external-api.com/posts/${params.id}`);
const post = await data.json();
return {
props: {
post,
},
}
}
export default PostPage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment