From 62ac743f793e4712c9ce0787b8c6c66032dfe78b Mon Sep 17 00:00:00 2001 From: Guus van Meerveld Date: Tue, 14 Feb 2023 17:39:27 +0100 Subject: [PATCH] Switched to getStaticProps for fetching individiual blog posts --- src/pages/blog/[id].tsx | 29 ++++++++++++++++++++--------- src/pages/blog/index.tsx | 18 +++++++++--------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/pages/blog/[id].tsx b/src/pages/blog/[id].tsx index e14a09c..04aa6e7 100644 --- a/src/pages/blog/[id].tsx +++ b/src/pages/blog/[id].tsx @@ -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 ( @@ -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; diff --git a/src/pages/blog/index.tsx b/src/pages/blog/index.tsx index 0db40ba..79e6a03 100644 --- a/src/pages/blog/index.tsx +++ b/src/pages/blog/index.tsx @@ -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 }; };