Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created March 23, 2022 12:54
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/9e5485e23989c2012784f158e5cc346c to your computer and use it in GitHub Desktop.
Save tsh-code/9e5485e23989c2012784f158e5cc346c to your computer and use it in GitHub Desktop.
isr.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: 'blocking'
}
}
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,
},
revalidate: 10, // In seconds
}
}
export default PostPage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment