Created
March 23, 2022 12:54
-
-
Save tsh-code/9e5485e23989c2012784f158e5cc346c to your computer and use it in GitHub Desktop.
isr.tsx
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
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