Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
59 lines
2.5 KiB
TypeScript
59 lines
2.5 KiB
TypeScript
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 },
|
|
});
|
|
}
|