Switched blog index page to getStaticProps

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

@ -1,5 +1,5 @@
import Link from "next/link"; import Link from "next/link";
import { NextPage } from "next"; import { GetStaticProps, NextPage } from "next";
import { NextSeo } from "next-seo"; import { NextSeo } from "next-seo";
import { Post, User } from "@prisma/client"; import { Post, User } from "@prisma/client";
@ -17,8 +17,7 @@ const Blog: NextPage<{
posts: (Post & { posts: (Post & {
author: User; author: User;
})[]; })[];
user: User | null; }> = ({ posts }) => {
}> = ({ posts, user }) => {
return ( return (
<Layout> <Layout>
<NextSeo title="Blog" /> <NextSeo title="Blog" />
@ -30,14 +29,7 @@ const Blog: NextPage<{
<div className="divider" /> <div className="divider" />
</div> </div>
</div> </div>
{user && (
<div className="columns">
<div className="column col-8 col-md-12 col-mx-auto">
<Link href="/blog/new">New post</Link> &middot;{" "}
<Link href="/api/blog/logout">Logout</Link>
</div>
</div>
)}
{posts.length < 1 && ( {posts.length < 1 && (
<div className="columns"> <div className="columns">
<div className="column col-8 col-md-12 col-mx-auto"> <div className="column col-8 col-md-12 col-mx-auto">
@ -53,20 +45,18 @@ const Blog: NextPage<{
); );
}; };
export const getServerSideProps = withSessionSsr( export const getStaticProps: GetStaticProps = async (
async ({ {
req
// query // query
}) => { }
const user = req.session.user ?? null; ) => {
// let cursor = 0; // let cursor = 0;
// if (!Array.isArray(query.cursor) && query.cursor !== undefined) { // if (!Array.isArray(query.cursor) && query.cursor !== undefined) {
// cursor = parseInt(query.cursor); // cursor = parseInt(query.cursor);
// } // }
const posts = await prisma.post.findMany({ const posts = await prisma.post.findMany({
where: { published: user ? undefined : true }, where: { published: true },
orderBy: { createdAt: "desc" }, orderBy: { createdAt: "desc" },
take: 5, take: 5,
include: { author: true } include: { author: true }
@ -74,15 +64,14 @@ export const getServerSideProps = withSessionSsr(
return { return {
props: { props: {
user,
posts: posts.map((post) => ({ posts: posts.map((post) => ({
...post, ...post,
createdAt: post.createdAt.toString(), createdAt: post.createdAt.toString(),
content: post.content?.split("\n")[0] content: post.content?.split("\n")[0]
})) }))
} },
revalidate: 30 * 60
};
}; };
}
);
export default Blog; export default Blog;

Loading…
Cancel
Save