Switched to getStaticProps for fetching individiual blog posts
continuous-integration/drone/push Build is passing Details

main
Guus van Meerveld 2 years ago
parent de66d634a8
commit 62ac743f79
Signed by: Guusvanmeerveld
GPG Key ID: 2BA7D7912771966E

@ -1,12 +1,11 @@
import { NextSeo } from "next-seo"; import { NextSeo } from "next-seo";
import { NextPage } from "next"; import { GetStaticPaths, GetStaticProps, NextPage } from "next";
import { Post, User } from "@prisma/client"; import { Post, User } from "@prisma/client";
import Layout from "@components/Layout"; import Layout from "@components/Layout";
import Tags from "@components/Tags"; import Tags from "@components/Tags";
import { withSessionSsr } from "@utils/session";
import prisma from "@utils/prisma"; import prisma from "@utils/prisma";
import styles from "./[id].module.scss"; import styles from "./[id].module.scss";
@ -15,7 +14,6 @@ const PostPage: NextPage<{
post: Post & { post: Post & {
author: User; author: User;
}; };
user: User | null;
}> = ({ post }) => { }> = ({ post }) => {
return ( return (
<Layout> <Layout>
@ -42,7 +40,22 @@ const PostPage: NextPage<{
); );
}; };
export const getServerSideProps = withSessionSsr(async ({ req, params }) => { export const getStaticPaths: GetStaticPaths = async ({}) => {
const posts = await prisma.post
.findMany({
where: { published: true }
})
.catch(() => []);
const paths = posts.map((post) => ({
params: { id: post.id.toString() }
}));
// { fallback: false } means other routes should 404
return { paths, fallback: false };
};
export const getStaticProps: GetStaticProps = async ({ params }) => {
if (!params?.id || Array.isArray(params?.id)) return { notFound: true }; if (!params?.id || Array.isArray(params?.id)) return { notFound: true };
const postId = parseInt(params?.id); const postId = parseInt(params?.id);
@ -56,17 +69,15 @@ export const getServerSideProps = withSessionSsr(async ({ req, params }) => {
if (post === null) return { notFound: true }; if (post === null) return { notFound: true };
const user = req.session.user ?? null;
return { return {
props: { props: {
user,
post: { post: {
...post, ...post,
createdAt: post.createdAt.toString() createdAt: post.createdAt.toString()
} }
} },
revalidate: 60 * 10
};
}; };
});
export default PostPage; export default PostPage;

@ -1,4 +1,3 @@
import Link from "next/link";
import { GetStaticProps, NextPage } from "next"; import { GetStaticProps, NextPage } from "next";
import { NextSeo } from "next-seo"; import { NextSeo } from "next-seo";
@ -6,7 +5,6 @@ import { Post, User } from "@prisma/client";
import Layout from "@components/Layout"; import Layout from "@components/Layout";
import { withSessionSsr } from "@utils/session";
import prisma from "@utils/prisma"; import prisma from "@utils/prisma";
import PostComponent from "@components/Post"; import PostComponent from "@components/Post";
@ -55,12 +53,14 @@ export const getStaticProps: GetStaticProps = async (
// cursor = parseInt(query.cursor); // cursor = parseInt(query.cursor);
// } // }
const posts = await prisma.post.findMany({ const posts = await prisma.post
.findMany({
where: { published: true }, where: { published: true },
orderBy: { createdAt: "desc" }, orderBy: { createdAt: "desc" },
take: 5, take: 5,
include: { author: true } include: { author: true }
}); })
.catch(() => []);
return { return {
props: { props: {
@ -70,7 +70,7 @@ export const getStaticProps: GetStaticProps = async (
content: post.content?.split("\n")[0] content: post.content?.split("\n")[0]
})) }))
}, },
revalidate: 30 * 60 revalidate: 60 * 1
}; };
}; };

Loading…
Cancel
Save