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

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

@ -1,12 +1,11 @@
import { NextSeo } from "next-seo";
import { NextPage } from "next";
import { GetStaticPaths, GetStaticProps, NextPage } from "next";
import { Post, User } from "@prisma/client";
import Layout from "@components/Layout";
import Tags from "@components/Tags";
import { withSessionSsr } from "@utils/session";
import prisma from "@utils/prisma";
import styles from "./[id].module.scss";
@ -15,7 +14,6 @@ const PostPage: NextPage<{
post: Post & {
author: User;
};
user: User | null;
}> = ({ post }) => {
return (
<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 };
const postId = parseInt(params?.id);
@ -56,17 +69,15 @@ export const getServerSideProps = withSessionSsr(async ({ req, params }) => {
if (post === null) return { notFound: true };
const user = req.session.user ?? null;
return {
props: {
user,
post: {
...post,
createdAt: post.createdAt.toString()
}
}
},
revalidate: 60 * 10
};
});
};
export default PostPage;

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

Loading…
Cancel
Save