Skip to content

Commit

Permalink
null blog fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Zain-ul-din committed Oct 25, 2024
1 parent 58bbd5e commit e9ddaef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/pages/blogs/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Flex } from '@chakra-ui/react';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import { useRouter } from 'next/router';
import { useEffect } from 'react';

interface Post {
content: {
Expand All @@ -26,15 +28,24 @@ export const getStaticPaths = (async () => {
export const getStaticProps = (async (context: GetStaticPropsContext) => {
const id = context.params!.id;
const res = await fetch(`https://sneakword.online/wp-json/wp/v2/posts/${id}`);
const post = (await res.json()) as Post;
const post = (await res.json()) as Post | undefined;
return {
props: { post: post }
props: { post: post },
revalidate: 60 * 60 * 12
};
}) satisfies GetStaticProps<{
post: Post;
post: Post | undefined;
}>;

export default function BlogDetailPage({ post }: { post: Post }) {
const router = useRouter();

useEffect(() => {
if (!post) router.push('/blogs');
}, [post, router]);

if (!post) return <></>;

return (
<Flex maxWidth={'1250px'} mx={'auto'} w={'full'} p={2}>
<div
Expand Down
1 change: 0 additions & 1 deletion src/pages/blogs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const getStaticProps = (async (context) => {
}>;

export default function Page({ posts }: InferGetStaticPropsType<typeof getStaticProps>) {
console.log(posts);
return (
<Flex maxWidth={'1200px'} mx={'auto'} w={'full'} p={2} justify={'center'} flexDir={'column'}>
<Stack mt={6}>
Expand Down

0 comments on commit e9ddaef

Please sign in to comment.