Skip to content

Instantly share code, notes, and snippets.

@szaranger
Created August 6, 2022 11:00
Show Gist options
  • Save szaranger/44a9d6371f0b2a0982e203563208784e to your computer and use it in GitHub Desktop.
Save szaranger/44a9d6371f0b2a0982e203563208784e to your computer and use it in GitHub Desktop.
import { useRouter } from 'next/router';
const Post = ({ post }) => {
const router = useRouter();
if (router.isFallback) {
return <div>Loading...</div>;
}
return <div>{...post}</div>;
};
export async function getStaticPaths() {
return {
paths: [{ params: { id: '1' } }, { params: { id: '2' } }],
fallback: true,
};
}
export async function getStaticProps({ params }) {
// if url was /posts/2, params.id will be 2
const res = await fetch(`https://path/to/your/api/${params.id}`);
const post = await res.json();
return { props: { post } };
}
export default Post;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment