Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
use ssr for post page
Browse files Browse the repository at this point in the history
  • Loading branch information
ceolinwill committed Jun 26, 2020
1 parent 4017784 commit a6c4480
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/pages/posts/[id]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from 'next';
import { GetServerSideProps, InferGetServerSidePropsType } from 'next';
import dynamic from 'next/dynamic';
import Error from 'next/error';
import { useRouter } from 'next/router';
import {
CircularProgress,
Container,
Divider,
makeStyles,
} from '@material-ui/core';
import { Container, Divider, makeStyles } from '@material-ui/core';
import LinkList from '@zoonk/components/LinkList';
import Meta from '@zoonk/components/Meta';
import PostBar from '@zoonk/components/PostBar';
Expand All @@ -16,7 +10,7 @@ import PostHeader from '@zoonk/components/PostHeader';
import RichTextViewer from '@zoonk/components/rich-text/RichTextViewer';
import { getPlainText, getPostImage } from '@zoonk/components/rich-text/posts';
import { Post } from '@zoonk/models';
import { getPost, getPosts } from '@zoonk/services';
import { getPost } from '@zoonk/services';
import { appLanguage, PostContext } from '@zoonk/utils';

const CommentList = dynamic(() => import('@zoonk/components/CommentList'), {
Expand All @@ -40,25 +34,19 @@ const useStyles = makeStyles((theme) => ({
},
}));

export const getStaticPaths: GetStaticPaths = async () => {
const posts = await getPosts({ limit: 50 });
const paths = posts.map((post) => ({ params: { id: post.id } }));
return { paths, fallback: true };
};

export const getStaticProps: GetStaticProps<PostPageProps> = async ({
export const getServerSideProps: GetServerSideProps<PostPageProps> = async ({
params,
}) => {
const id = String(params?.id);
const data = await getPost(id);
return { props: { data }, unstable_revalidate: 1 };
return { props: { data } };
};

const PostPage = ({ data }: InferGetStaticPropsType<typeof getStaticProps>) => {
const { isFallback } = useRouter();
const PostPage = ({
data,
}: InferGetServerSidePropsType<typeof getServerSideProps>) => {
const classes = useStyles();

if (!data && isFallback) return <CircularProgress />;
if (!data) return <Error statusCode={404} />;

const { content, cover, id, language, sites, title } = data;
Expand Down

0 comments on commit a6c4480

Please sign in to comment.