Last active
November 5, 2020 16:21
-
-
Save laiso/996449c0d3d23249e61e783d1d3ac1b0 to your computer and use it in GitHub Desktop.
SSG+CSR #CodePiece
This file contains hidden or 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/posts/[id].js | |
export default function Page(props) { | |
const router = useRouter() | |
const [post, setPost] = useState(props.post) | |
if (!post) { | |
return <Locading /> | |
} | |
useEffect(async () => { | |
if (!post) { | |
const resp = fetch(`https://example.com/api/post/${router.params.id}`) | |
setPost(resp.data) | |
} | |
}) | |
return <> | |
<PostBody data={post} /> | |
</> | |
} | |
export async function getStaticPaths() { | |
const resp = await fetch(`https://example.com/api/posts`) | |
const paths = resp.data.map(post => ({ | |
params: { | |
id: post.id, | |
} | |
})) | |
return { | |
paths, | |
fallback: true, | |
} | |
} | |
export async function getStaticProps(ctx) { | |
const resp = await fetch(`https://example.com/api/post/${ctx.params.id}`) | |
const post = resp.data | |
return { | |
props: { | |
post, | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment