Skip to content

Commit

Permalink
add new sanityFetch helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchuman committed Nov 5, 2024
1 parent d34c4a3 commit 9397ab4
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanitypress",
"version": "4.12.2",
"version": "4.12.3",
"description": "A Next.js + Sanity.io Starter Template",
"author": "nuotsu <[email protected]> (https://nuotsu.dev)",
"license": "ISC",
Expand Down
6 changes: 2 additions & 4 deletions src/app/(frontend)/[...slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '@/sanity/client'
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import { modulesQuery } from '@/sanity/lib/queries'
import { notFound } from 'next/navigation'
import Modules from '@/ui/modules'
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function generateStaticParams() {
}

async function getPage(params: { slug?: string[] }) {
const { data } = await sanityFetch({
return await fetchSanityLive<Sanity.Page>({
query: groq`*[
_type == 'page' &&
metadata.slug.current == $slug &&
Expand All @@ -45,8 +45,6 @@ async function getPage(params: { slug?: string[] }) {
}`,
params: { slug: params.slug?.join('/') },
})

return data as Sanity.Page
}

type Props = {
Expand Down
10 changes: 3 additions & 7 deletions src/app/(frontend)/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import client from '@/sanity/client'
import { sanityFetch, groq } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import { modulesQuery } from '@/sanity/lib/queries'
import { notFound } from 'next/navigation'
import Modules from '@/ui/modules'
Expand Down Expand Up @@ -27,7 +27,7 @@ export async function generateStaticParams() {
}

async function getPost(params: { slug?: string }) {
const { data } = await sanityFetch({
return await fetchSanityLive<Sanity.BlogPost>({
query: groq`*[_type == 'blog.post' && metadata.slug.current == $slug][0]{
...,
'body': select(_type == 'image' => asset->, body),
Expand All @@ -45,20 +45,16 @@ async function getPost(params: { slug?: string }) {
}`,
params,
})

return data as Sanity.BlogPost
}

async function getPageTemplate() {
const { data } = await sanityFetch({
return await fetchSanityLive<Sanity.Page>({
query: groq`*[_type == 'page' && metadata.slug.current == 'blog/*'][0]{
...,
modules[]{ ${modulesQuery} },
metadata { slug }
}`,
})

return data as Sanity.Page
}

type Props = {
Expand Down
14 changes: 6 additions & 8 deletions src/app/(frontend)/blog/rss.xml/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import processUrl from '@/lib/processUrl'
import { Feed } from 'feed'
import { escapeHTML, toHTML } from '@portabletext/to-html'
import { urlFor } from '@/sanity/lib/image'

export async function GET() {
const { data } = await sanityFetch({
const { blog, posts, copyright } = await fetchSanityLive<{
blog: Sanity.Page
posts: Array<Sanity.BlogPost & { image?: string }>
copyright: string
}>({
query: groq`{
'blog': *[_type == 'page' && metadata.slug.current == 'blog'][0]{
_type,
Expand All @@ -24,12 +28,6 @@ export async function GET() {
}`,
})

const { blog, posts, copyright } = data as {
blog: Sanity.Page
posts: Array<Sanity.BlogPost & { image?: string }>
copyright: string
}

if (!blog || !posts) {
return new Response(
'Missing either a blog page or blog posts in Sanity Studio',
Expand Down
6 changes: 2 additions & 4 deletions src/app/(frontend)/not-found.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import { modulesQuery } from '@/sanity/lib/queries'
import Modules from '@/ui/modules'

Expand All @@ -13,12 +13,10 @@ export async function generateMetadata() {
}

async function get404() {
const { data } = await sanityFetch({
return await fetchSanityLive<Sanity.Page>({
query: groq`*[_type == 'page' && metadata.slug.current == '404'][0]{
...,
modules[]{ ${modulesQuery} }
}`,
})

return data as Sanity.Page
}
6 changes: 3 additions & 3 deletions src/app/(frontend)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sanityFetch, groq } from '@/sanity/lib/fetch'
import { groq, fetchSanityLive } from '@/sanity/lib/fetch'
import { modulesQuery } from '@/sanity/lib/queries'
import Modules from '@/ui/modules'
import processMetadata from '@/lib/processMetadata'
Expand All @@ -14,7 +14,7 @@ export async function generateMetadata() {
}

async function getPage() {
const { data } = await sanityFetch({
const data = await fetchSanityLive<Sanity.Page>({
query: groq`*[_type == 'page' && metadata.slug.current == 'index'][0]{
...,
modules[]{ ${modulesQuery} },
Expand All @@ -30,5 +30,5 @@ async function getPage() {
"Missing 'page' document with metadata.slug 'index' in Sanity Studio",
)

return data as Sanity.Page
return data
}
6 changes: 3 additions & 3 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import type { MetadataRoute } from 'next'

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const { data } = await sanityFetch({
const data = await fetchSanityLive<Record<string, MetadataRoute.Sitemap>>({
query: groq`{
'pages': *[
_type == 'page' &&
Expand All @@ -27,5 +27,5 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
},
})

return Object.values(data as Record<string, MetadataRoute.Sitemap>).flat()
return Object.values(data).flat()
}
11 changes: 11 additions & 0 deletions src/sanity/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,14 @@ export const { sanityFetch, SanityLive } = defineLive({
revalidate: dev ? 0 : 3600,
},
})

export async function fetchSanityLive<T = any>(
args: Parameters<typeof sanityFetch>[0],
) {
const { data } = await sanityFetch({
...args,
perspective: dev ? 'previewDrafts' : 'published',
})

return data as T
}
6 changes: 3 additions & 3 deletions src/sanity/lib/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { groq, sanityFetch } from './fetch'
import { fetchSanityLive, groq } from './fetch'

export const linkQuery = groq`
...,
Expand All @@ -20,7 +20,7 @@ export const ctaQuery = groq`
`

export async function getSite() {
const { data } = await sanityFetch({
const data = await fetchSanityLive<Sanity.Site>({
query: groq`
*[_type == 'site'][0]{
...,
Expand All @@ -35,7 +35,7 @@ export async function getSite() {

if (!data) throw new Error("Missing 'site' document in Sanity Studio")

return data as Sanity.Site
return data
}

export const reputationBlockQuery = groq`
Expand Down
7 changes: 4 additions & 3 deletions src/ui/modules/LogoList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import { PortableText } from 'next-sanity'
import Pretitle from '@/ui/Pretitle'
import Img from '@/ui/Img'
Expand All @@ -20,8 +20,9 @@ export default async function LogoList({
}>) {
const allLogos =
logos ||
((await sanityFetch({ query: groq`*[_type == 'logo']|order(name)` }))
.data as Sanity.Logo[])
(await fetchSanityLive<Sanity.Logo[]>({
query: groq`*[_type == 'logo']|order(name)`,
}))

return (
<section className="section space-y-8">
Expand Down
8 changes: 4 additions & 4 deletions src/ui/modules/blog/BlogFrontpage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import FilterList from '../BlogList/FilterList'
import PostPreviewLarge from '../PostPreviewLarge'
import Paginated from './Paginated'
Expand All @@ -14,7 +14,7 @@ export default async function BlogFrontpage({
showFeaturedPostsFirst: boolean
itemsPerPage: number
}>) {
const { data } = (await sanityFetch({
const posts = await fetchSanityLive<Sanity.BlogPost[]>({
query: groq`*[_type == 'blog.post']|order(publishDate desc){
_type,
_id,
Expand All @@ -25,10 +25,10 @@ export default async function BlogFrontpage({
publishDate,
}
`,
})) as { data: Sanity.BlogPost[] }
})

const [firstPost, ...otherPosts] =
stegaClean(mainPost) === 'featured' ? sortFeaturedPosts(data) : data
stegaClean(mainPost) === 'featured' ? sortFeaturedPosts(posts) : posts

return (
<section className="section space-y-12">
Expand Down
8 changes: 4 additions & 4 deletions src/ui/modules/blog/BlogList/FilterList.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import Filter from './Filter'
import css from './FilterList.module.css'
import { cn } from '@/lib/utils'

export default async function FilterList() {
const { data } = (await sanityFetch({
const categories = await fetchSanityLive<Sanity.BlogCategory[]>({
query: groq`*[
_type == 'blog.category' &&
count(*[_type == 'blog.post' && references(^._id)]) > 0
]|order(title)`,
})) as { data: Sanity.BlogCategory[] }
})

return (
<fieldset>
Expand All @@ -23,7 +23,7 @@ export default async function FilterList() {
>
<Filter label="All" />

{data?.map((category, key) => (
{categories?.map((category, key) => (
<Filter label={category.title} value={category._id} key={key} />
))}
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/ui/modules/blog/BlogList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { groq, sanityFetch } from '@/sanity/lib/fetch'
import { fetchSanityLive, groq } from '@/sanity/lib/fetch'
import { PortableText, stegaClean } from 'next-sanity'
import FilterList from '@/ui/modules/blog/BlogList/FilterList'
import List from './List'
Expand All @@ -19,7 +19,7 @@ export default async function BlogList({
displayFilters: boolean
filteredCategory: Sanity.BlogCategory
}>) {
const { data } = await sanityFetch({
const posts = await fetchSanityLive<Sanity.BlogPost[]>({
query: groq`
*[
_type == 'blog.post'
Expand Down Expand Up @@ -52,7 +52,7 @@ export default async function BlogList({
{displayFilters && !filteredCategory && <FilterList />}

<List
posts={data as Sanity.BlogPost[]}
posts={posts}
className={cn(
'items-stretch gap-x-8 gap-y-12',
stegaClean(layout) === 'grid'
Expand Down

0 comments on commit 9397ab4

Please sign in to comment.