next-sanity
Version:
Sanity.io toolkit for Next.js
103 lines (102 loc) • 4.94 kB
JavaScript
import { t as isCorsOriginError } from "../../../isCorsOriginError.js";
import "../../../constants.js";
import { i as preconnect, n as validateStrictFetchOptions, r as validateStrictSanityLiveProps, t as resolvePerspectiveFromCookies } from "../../../resolvePerspectiveFromCookies.js";
import { t as parseTags } from "../../../parseTags.js";
import { cookies, draftMode } from "next/headers";
import { jsx } from "react/jsx-runtime";
import { SanityLive } from "next-sanity/live/client-components";
import { revalidateSyncTagsAction } from "next-sanity/live/server-actions";
import { PHASE_PRODUCTION_BUILD } from "next/constants";
function defineLive(config) {
const { client: _client, serverToken, browserToken, strict = false } = config;
if (!_client) throw new Error("`client` is required for `defineLive` to function");
if (process.env.NODE_ENV === "development" && !serverToken && serverToken !== false) console.warn("No `serverToken` provided to `defineLive`. This means that only published content will be fetched and respond to live events. You can silence this warning by setting `serverToken: false`.");
if (process.env.NODE_ENV === "development" && !browserToken && browserToken !== false) console.warn("No `browserToken` provided to `defineLive`. This means that live previewing drafts will only work when using the Presentation Tool in your Sanity Studio. To support live previewing drafts stand-alone, provide a `browserToken`. It is shared with the browser so it should only have Viewer rights or lower. You can silence this warning by setting `browserToken: false`.");
const client = _client.withConfig({
allowReconfigure: false,
useCdn: true,
perspective: "published",
stega: false
});
const studioUrlDefined = typeof client.config().stega.studioUrl !== "undefined";
const sanityFetch = async function sanityFetch({ query, params = {}, perspective: _perspective, stega: _stega, tags = [], requestTag = "next-loader.fetch" }) {
if (strict) validateStrictFetchOptions({
perspective: _perspective,
stega: _stega
});
const stega = strict ? _stega : _stega ?? (serverToken && studioUrlDefined ? (await draftMode()).isEnabled : false);
const perspective = strict ? _perspective : _perspective ?? (serverToken ? await resolveCookiePerspective() : void 0);
const useCdn = perspective ? perspective === "published" : void 0;
const isBuildPhase = process.env["NEXT_PHASE"] === PHASE_PRODUCTION_BUILD;
const cacheMode = useCdn !== false && !isBuildPhase ? "noStale" : void 0;
const token = (perspective && perspective !== "published" || stega) && serverToken ? serverToken : void 0;
const { syncTags } = await client.fetch(query, await params, {
filterResponse: false,
perspective,
stega: false,
resultSourceMap: false,
returnQuery: false,
useCdn,
cacheMode,
tag: [requestTag, "fetch-sync-tags"].filter(Boolean).join("."),
token
});
const cacheTags = [...tags, ...syncTags?.map((tag) => `sanity:${tag}`) || []];
const { result, resultSourceMap } = await client.fetch(query, await params, {
filterResponse: false,
perspective,
stega,
next: {
revalidate: false,
tags: cacheTags
},
useCdn,
cacheMode,
tag: requestTag,
token
});
return {
data: result,
sourceMap: resultSourceMap || null,
tags: cacheTags
};
};
const SanityLive$2 = async function SanityLive$1(props) {
if (strict) validateStrictSanityLiveProps(props);
const { includeDrafts: _includeDrafts, requestTag = "next-loader.live", waitFor, action, onError, onWelcome, onReconnect, onRestart, onGoAway } = props;
const { projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix } = client.config();
const includeDrafts = typeof browserToken === "string" && !!browserToken && (_includeDrafts ?? (await draftMode()).isEnabled);
const shouldWaitFor = waitFor === "function" && !includeDrafts ? waitFor : void 0;
preconnect(client);
return /* @__PURE__ */ jsx(SanityLive, {
config: {
projectId,
dataset,
apiHost: apiHost === "https://api.sanity.io" ? void 0 : apiHost,
apiVersion,
useProjectHostname: useProjectHostname ? void 0 : useProjectHostname,
requestTagPrefix,
token: includeDrafts ? browserToken : void 0
},
includeDrafts: includeDrafts ? true : void 0,
requestTag,
waitFor: shouldWaitFor,
action: action ?? (shouldWaitFor === "function" || includeDrafts ? "refresh" : revalidateSyncTagsAction),
onError,
onWelcome,
onReconnect,
onRestart,
onGoAway
});
};
SanityLive$2.displayName = "SanityLiveServerComponent";
return {
sanityFetch,
SanityLive: SanityLive$2
};
}
async function resolveCookiePerspective() {
return (await draftMode()).isEnabled ? await resolvePerspectiveFromCookies({ cookies: await cookies() }) : void 0;
}
export { defineLive, isCorsOriginError, parseTags, resolvePerspectiveFromCookies };
//# sourceMappingURL=index.js.map