Last active
November 29, 2021 17:44
-
-
Save lumenwrites/83d8fcac2623de6901bc6e730db36b09 to your computer and use it in GitHub Desktop.
Vercel Throttling Issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. index.tsx has getServerSideProps() function which calls a getPosts() function to fetch the posts. | |
2. get-posts.ts runs a prisma query and fetches the posts. | |
3. it uses prismaClient |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Layout from 'components/Layout/Layout' | |
import PostFeed from 'components/Posts/PostFeed' | |
import Subnav from 'components/Layout/Subnav' | |
import Pagination from 'components/Posts/Pagination' | |
import ProfileHeader from 'components/Users/ProfileHeader' | |
import TagHeader from 'components/Layout/TagHeader' | |
import HomeHeader from 'components/CTAs/HomeHeader' | |
import SubscribeBox from 'components/CTAs/SubscribeBox' | |
import AdBoxes from 'components/CTAs/AdBoxes' | |
export default function browse({ posts, postCount, username }) { | |
return ( | |
<Layout subnav={<Subnav />}> | |
<PostFeed posts={posts} /> | |
<Pagination postCount={postCount} /> | |
<AdBoxes/> | |
<SubscribeBox /> | |
<br /> | |
</Layout> | |
) | |
} | |
import { getPosts } from 'prisma/api/posts/get-posts' | |
import config from 'config.json' | |
export async function getServerSideProps({ req, query }) { | |
const { username, sort, tag, search } = query | |
const { posts, postCount } = await getPosts({ | |
published: true, | |
searchString: search, | |
username: username, | |
tagSlug: tag, | |
sort: sort, | |
skip: config.postsPerPage * (parseInt(query.page?.toString()) - 1 || 0), | |
take: config.postsPerPage, | |
}) | |
return { props: { posts, postCount, username } } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-nocheck | |
import prisma from 'prisma/prismaClient' | |
export async function getPosts({ username, published, tagSlug, searchString, sort, skip, take }) { | |
console.log(`Get posts. Sorting: ${sort}`) | |
// Filter posts by user (to show them on their profile) | |
let author | |
if (username) author = await prisma.user.findUnique({ where: { username } }) | |
// Filter by tag | |
const tagFilter = tagSlug ? { | |
tags: { some: { slug: tagSlug } } | |
} : {} | |
// Search through posts | |
const search = searchString ? { | |
OR: [ | |
{ title: { contains: searchString, mode: "insensitive", } }, | |
{ body: { contains: searchString, mode: "insensitive", } }, | |
{ tags: { some: { name: { contains: searchString, mode: "insensitive", } } } }, | |
{ author: { username: { contains: searchString, mode: "insensitive", } } }, | |
], | |
} : {} | |
let orderBy = [{ rank: 'desc' }] | |
if (sort === 'new') orderBy = [{ createdAt: 'desc' }] | |
if (sort === 'top') orderBy = [{ score: 'desc' }] | |
const allFilters = { | |
authorId: author?.id, | |
published: published, | |
...search, | |
...tagFilter, | |
} | |
const [posts, postCount] = await prisma.$transaction([ | |
prisma.post.findMany({ | |
where: allFilters, | |
orderBy: orderBy, //rank: 'desc' //score: 'desc' | |
take, skip, | |
include: { | |
tags: true, | |
author: { | |
select: { | |
username: true | |
} | |
}, | |
upvoters: { | |
select: { | |
username: true | |
} | |
}, | |
// Just for the comment counter | |
comments: { | |
select: { | |
id: true | |
} | |
} | |
} | |
}), | |
prisma.post.count({ where: allFilters }) | |
]) | |
return { posts, postCount } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PrismaClient } from "@prisma/client"; | |
// PrismaClient is attached to the `global` object in development to prevent | |
// exhausting your database connection limit. | |
// | |
// Learn more: | |
// https://pris.ly/d/help/next-js-best-practices | |
let prisma: PrismaClient | |
if (process.env.NODE_ENV === 'production') { | |
prisma = new PrismaClient() | |
} else { | |
if (!global.prisma) { | |
global.prisma = new PrismaClient() | |
} | |
prisma = global.prisma | |
} | |
export default prisma |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment