File size: 2,538 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import type { ClientPerspective, QueryParams } from "next-sanity";
import { draftMode } from "next/headers";
import { client } from "@/sanity/lib/client";
import { token } from "@/sanity/lib/token";
/**
* Used to fetch data in Server Components, it has built in support for handling Draft Mode and perspectives.
* 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)
* and will also fetch from the CDN.
* When using the "previewDrafts" perspective then the data is fetched from the live API and isn't cached, it will also fetch draft content that isn't published yet.
*/
export async function sanityFetch<const QueryString extends string>({
query,
params = {},
perspective: _perspective,
/**
* Stega embedded Content Source Maps are used by Visual Editing by both the Sanity Presentation Tool and Vercel Visual Editing.
* The Sanity Presentation Tool will enable Draft Mode when loading up the live preview, and we use it as a signal for when to embed source maps.
* When outside of the Sanity Studio we also support the Vercel Toolbar Visual Editing feature, which is only enabled in production when it's a Vercel Preview Deployment.
*/
stega: _stega,
}: {
query: QueryString;
params?: QueryParams | Promise<QueryParams>;
perspective?: Omit<ClientPerspective, "raw">;
stega?: boolean;
}) {
const perspective =
_perspective || (await draftMode()).isEnabled
? "previewDrafts"
: "published";
const stega =
_stega ||
perspective === "previewDrafts" ||
process.env.VERCEL_ENV === "preview";
if (perspective === "previewDrafts") {
return client.fetch(query, await params, {
stega,
perspective: "previewDrafts",
// The token is required to fetch draft content
token,
// The `previewDrafts` perspective isn't available on the API CDN
useCdn: false,
// And we can't cache the responses as it would slow down the live preview experience
next: { revalidate: 0 },
});
}
return client.fetch(query, await params, {
stega,
perspective: "published",
// The `published` perspective is available on the API CDN
useCdn: true,
// Only enable Stega in production if it's a Vercel Preview Deployment, as the Vercel Toolbar supports Visual Editing
// When using the `published` perspective we use time-based revalidation to match the time-to-live on Sanity's API CDN (60 seconds)
next: { revalidate: 60 },
});
}
|