1- import type { ClientPerspective , QueryParams } from "next-sanity" ;
1+ import type { ClientPerspective , FilteredResponseQueryOptions , QueryParams } from "next-sanity" ;
22import { draftMode } from "next/headers" ;
33
44import { client } from "@/sanity/lib/client" ;
55import { token } from "@/sanity/lib/token" ;
6- console . debug ( 'env' , process . env . VERCEL_ENV )
76/**
87 * Used to fetch data in Server Components, it has built in support for handling Draft Mode and perspectives.
98 * When using the "published" perspective then time-based revalidation is used, set to match the time-to-live on Sanity's API CDN (60 seconds)
@@ -26,8 +25,19 @@ export async function sanityFetch<QueryResponse>({
2625 perspective ?: Omit < ClientPerspective , "raw" > ;
2726 stega ?: boolean ;
2827} ) {
28+
29+ let config : FilteredResponseQueryOptions = {
30+ stega,
31+ perspective : "published" ,
32+ // The `published` perspective is available on the API CDN
33+ useCdn : true ,
34+ // Only enable Stega in production if it's a Vercel Preview Deployment, as the Vercel Toolbar supports Visual Editing
35+ // When using the `published` perspective we use time-based revalidation to match the time-to-live on Sanity's API CDN (60 seconds)
36+ next : { revalidate : 60 } ,
37+ }
38+
2939 if ( perspective === "previewDrafts" ) {
30- return client . fetch < QueryResponse > ( query , params , {
40+ config = {
3141 stega,
3242 perspective : "previewDrafts" ,
3343 // The token is required to fetch draft content
@@ -36,15 +46,16 @@ export async function sanityFetch<QueryResponse>({
3646 useCdn : false ,
3747 // And we can't cache the responses as it would slow down the live preview experience
3848 next : { revalidate : 0 } ,
39- } ) ;
49+ }
4050 }
41- return client . fetch < QueryResponse > ( query , params , {
42- stega,
43- perspective : "published" ,
44- // The `published` perspective is available on the API CDN
45- useCdn : true ,
46- // Only enable Stega in production if it's a Vercel Preview Deployment, as the Vercel Toolbar supports Visual Editing
47- // When using the `published` perspective we use time-based revalidation to match the time-to-live on Sanity's API CDN (60 seconds)
48- next : { revalidate : 60 } ,
49- } ) ;
51+
52+
53+ console . debug ( {
54+ // replace "\n" with carriage return
55+ query : query . replace ( / \n / g, "" ) ,
56+ params,
57+ config
58+ } )
59+
60+ return client . fetch < QueryResponse > ( query , params , config ) ;
5061}
0 commit comments