From 9397ab48157ba03b3c604c4f00ccd2e73bb2211c Mon Sep 17 00:00:00 2001 From: Mitchell Christ Date: Mon, 4 Nov 2024 18:20:00 -0800 Subject: [PATCH] add new sanityFetch helper --- package.json | 2 +- src/app/(frontend)/[...slug]/page.tsx | 6 ++---- src/app/(frontend)/blog/[slug]/page.tsx | 10 +++------- src/app/(frontend)/blog/rss.xml/route.ts | 14 ++++++-------- src/app/(frontend)/not-found.tsx | 6 ++---- src/app/(frontend)/page.tsx | 6 +++--- src/app/sitemap.ts | 6 +++--- src/sanity/lib/fetch.ts | 11 +++++++++++ src/sanity/lib/queries.ts | 6 +++--- src/ui/modules/LogoList.tsx | 7 ++++--- src/ui/modules/blog/BlogFrontpage/index.tsx | 8 ++++---- src/ui/modules/blog/BlogList/FilterList.tsx | 8 ++++---- src/ui/modules/blog/BlogList/index.tsx | 6 +++--- 13 files changed, 49 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index 2a483e2..d8e2415 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sanitypress", - "version": "4.12.2", + "version": "4.12.3", "description": "A Next.js + Sanity.io Starter Template", "author": "nuotsu (https://nuotsu.dev)", "license": "ISC", diff --git a/src/app/(frontend)/[...slug]/page.tsx b/src/app/(frontend)/[...slug]/page.tsx index 459afed..a768b75 100644 --- a/src/app/(frontend)/[...slug]/page.tsx +++ b/src/app/(frontend)/[...slug]/page.tsx @@ -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' @@ -30,7 +30,7 @@ export async function generateStaticParams() { } async function getPage(params: { slug?: string[] }) { - const { data } = await sanityFetch({ + return await fetchSanityLive({ query: groq`*[ _type == 'page' && metadata.slug.current == $slug && @@ -45,8 +45,6 @@ async function getPage(params: { slug?: string[] }) { }`, params: { slug: params.slug?.join('/') }, }) - - return data as Sanity.Page } type Props = { diff --git a/src/app/(frontend)/blog/[slug]/page.tsx b/src/app/(frontend)/blog/[slug]/page.tsx index ad6569d..b2c68bd 100644 --- a/src/app/(frontend)/blog/[slug]/page.tsx +++ b/src/app/(frontend)/blog/[slug]/page.tsx @@ -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' @@ -27,7 +27,7 @@ export async function generateStaticParams() { } async function getPost(params: { slug?: string }) { - const { data } = await sanityFetch({ + return await fetchSanityLive({ query: groq`*[_type == 'blog.post' && metadata.slug.current == $slug][0]{ ..., 'body': select(_type == 'image' => asset->, body), @@ -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({ query: groq`*[_type == 'page' && metadata.slug.current == 'blog/*'][0]{ ..., modules[]{ ${modulesQuery} }, metadata { slug } }`, }) - - return data as Sanity.Page } type Props = { diff --git a/src/app/(frontend)/blog/rss.xml/route.ts b/src/app/(frontend)/blog/rss.xml/route.ts index be88b79..b0c5d7a 100644 --- a/src/app/(frontend)/blog/rss.xml/route.ts +++ b/src/app/(frontend)/blog/rss.xml/route.ts @@ -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 + copyright: string + }>({ query: groq`{ 'blog': *[_type == 'page' && metadata.slug.current == 'blog'][0]{ _type, @@ -24,12 +28,6 @@ export async function GET() { }`, }) - const { blog, posts, copyright } = data as { - blog: Sanity.Page - posts: Array - copyright: string - } - if (!blog || !posts) { return new Response( 'Missing either a blog page or blog posts in Sanity Studio', diff --git a/src/app/(frontend)/not-found.tsx b/src/app/(frontend)/not-found.tsx index 1828a1b..3ab05d4 100644 --- a/src/app/(frontend)/not-found.tsx +++ b/src/app/(frontend)/not-found.tsx @@ -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' @@ -13,12 +13,10 @@ export async function generateMetadata() { } async function get404() { - const { data } = await sanityFetch({ + return await fetchSanityLive({ query: groq`*[_type == 'page' && metadata.slug.current == '404'][0]{ ..., modules[]{ ${modulesQuery} } }`, }) - - return data as Sanity.Page } diff --git a/src/app/(frontend)/page.tsx b/src/app/(frontend)/page.tsx index 0f8f5a6..c7e7e6b 100644 --- a/src/app/(frontend)/page.tsx +++ b/src/app/(frontend)/page.tsx @@ -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' @@ -14,7 +14,7 @@ export async function generateMetadata() { } async function getPage() { - const { data } = await sanityFetch({ + const data = await fetchSanityLive({ query: groq`*[_type == 'page' && metadata.slug.current == 'index'][0]{ ..., modules[]{ ${modulesQuery} }, @@ -30,5 +30,5 @@ async function getPage() { "Missing 'page' document with metadata.slug 'index' in Sanity Studio", ) - return data as Sanity.Page + return data } diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts index f045b2e..8fb217e 100644 --- a/src/app/sitemap.ts +++ b/src/app/sitemap.ts @@ -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 { - const { data } = await sanityFetch({ + const data = await fetchSanityLive>({ query: groq`{ 'pages': *[ _type == 'page' && @@ -27,5 +27,5 @@ export default async function sitemap(): Promise { }, }) - return Object.values(data as Record).flat() + return Object.values(data).flat() } diff --git a/src/sanity/lib/fetch.ts b/src/sanity/lib/fetch.ts index ced8ba2..d4d5989 100644 --- a/src/sanity/lib/fetch.ts +++ b/src/sanity/lib/fetch.ts @@ -50,3 +50,14 @@ export const { sanityFetch, SanityLive } = defineLive({ revalidate: dev ? 0 : 3600, }, }) + +export async function fetchSanityLive( + args: Parameters[0], +) { + const { data } = await sanityFetch({ + ...args, + perspective: dev ? 'previewDrafts' : 'published', + }) + + return data as T +} diff --git a/src/sanity/lib/queries.ts b/src/sanity/lib/queries.ts index 41c3599..ce4a969 100644 --- a/src/sanity/lib/queries.ts +++ b/src/sanity/lib/queries.ts @@ -1,4 +1,4 @@ -import { groq, sanityFetch } from './fetch' +import { fetchSanityLive, groq } from './fetch' export const linkQuery = groq` ..., @@ -20,7 +20,7 @@ export const ctaQuery = groq` ` export async function getSite() { - const { data } = await sanityFetch({ + const data = await fetchSanityLive({ query: groq` *[_type == 'site'][0]{ ..., @@ -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` diff --git a/src/ui/modules/LogoList.tsx b/src/ui/modules/LogoList.tsx index cbf23d9..5497c38 100644 --- a/src/ui/modules/LogoList.tsx +++ b/src/ui/modules/LogoList.tsx @@ -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' @@ -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({ + query: groq`*[_type == 'logo']|order(name)`, + })) return (
diff --git a/src/ui/modules/blog/BlogFrontpage/index.tsx b/src/ui/modules/blog/BlogFrontpage/index.tsx index 50765f9..c7de0b2 100644 --- a/src/ui/modules/blog/BlogFrontpage/index.tsx +++ b/src/ui/modules/blog/BlogFrontpage/index.tsx @@ -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' @@ -14,7 +14,7 @@ export default async function BlogFrontpage({ showFeaturedPostsFirst: boolean itemsPerPage: number }>) { - const { data } = (await sanityFetch({ + const posts = await fetchSanityLive({ query: groq`*[_type == 'blog.post']|order(publishDate desc){ _type, _id, @@ -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 (
diff --git a/src/ui/modules/blog/BlogList/FilterList.tsx b/src/ui/modules/blog/BlogList/FilterList.tsx index 5afda77..4dfe4ff 100644 --- a/src/ui/modules/blog/BlogList/FilterList.tsx +++ b/src/ui/modules/blog/BlogList/FilterList.tsx @@ -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({ query: groq`*[ _type == 'blog.category' && count(*[_type == 'blog.post' && references(^._id)]) > 0 ]|order(title)`, - })) as { data: Sanity.BlogCategory[] } + }) return (
@@ -23,7 +23,7 @@ export default async function FilterList() { > - {data?.map((category, key) => ( + {categories?.map((category, key) => ( ))} diff --git a/src/ui/modules/blog/BlogList/index.tsx b/src/ui/modules/blog/BlogList/index.tsx index 7bf0eca..cda4a73 100644 --- a/src/ui/modules/blog/BlogList/index.tsx +++ b/src/ui/modules/blog/BlogList/index.tsx @@ -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' @@ -19,7 +19,7 @@ export default async function BlogList({ displayFilters: boolean filteredCategory: Sanity.BlogCategory }>) { - const { data } = await sanityFetch({ + const posts = await fetchSanityLive({ query: groq` *[ _type == 'blog.post' @@ -52,7 +52,7 @@ export default async function BlogList({ {displayFilters && !filteredCategory && }