Skip to content

Commit

Permalink
limit posts on homepage and show all on blog
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash Smith committed Sep 12, 2023
1 parent 4ebfb7b commit 6301ab6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/lib/sanity.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import type { ImageAsset, Slug } from '@sanity/types'
import groq from 'groq'
import { type SanityClient } from 'next-sanity'

export const postsQuery = groq`*[_type == "post" && defined(slug.current)] | order(_createdAt desc)`
export const postsQuery = groq`*[_type == "post" && defined(slug.current)][0..5] | order(_createdAt desc)`
export const allPostsQuery = groq`*[_type == "post" && defined(slug.current)] | order(_createdAt desc)`

export async function getPosts(client: SanityClient): Promise<Post[]> {
return await client.fetch(postsQuery)
}

export async function getAllPosts(client: SanityClient): Promise<Post[]> {
return await client.fetch(allPostsQuery)
}

export const postBySlugQuery = groq`*[_type == "post" && slug.current == $slug][0]`

export const homepageQuery = groq`*[_type == "home"][0]`
Expand Down
4 changes: 2 additions & 2 deletions src/pages/blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Card from '~/components/Card'
import Container from '~/components/Container'
import { readToken } from '~/lib/sanity.api'
import { getClient } from '~/lib/sanity.client'
import { getPosts, type Post, postsQuery } from '~/lib/sanity.queries'
import { getAllPosts, type Post, postsQuery } from '~/lib/sanity.queries'
import type { SharedPageProps } from '~/pages/_app'

export const getStaticProps: GetStaticProps<
Expand All @@ -15,7 +15,7 @@ export const getStaticProps: GetStaticProps<
}
> = async ({ draftMode = false }) => {
const client = getClient(draftMode ? { token: readToken } : undefined)
const posts = await getPosts(client)
const posts = await getAllPosts(client)

return {
props: {
Expand Down

0 comments on commit 6301ab6

Please sign in to comment.