next-sanity
Version:
Sanity.io toolkit for Next.js
95 lines (94 loc) • 4.75 kB
JavaScript
import { t as cacheTagPrefix } from "../../../constants.js";
import { t as isCorsOriginError } from "../../../isCorsOriginError.js";
import { a as preconnect, i as validateStrictSanityLiveProps, n as resolvePerspectiveFromCookies, r as validateStrictFetchOptions, t as resolveVariantFromCookies } from "../../../resolveVariantFromCookies.js";
import { t as parseTags } from "../../../parseTags.js";
import { jsx } from "react/jsx-runtime";
import { sanity } from "next-sanity/live/cache-life";
import { SanityLive } from "next-sanity/live/client-components";
import { revalidateSyncTagsAction } from "next-sanity/live/server-actions";
import { cacheLife, cacheTag } from "next/cache";
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 sanityFetch = async function sanityFetch({ query, params = {}, perspective, variant, stega, tags: customCacheTags = [], requestTag = "next-loader.fetch.cache-components" }) {
if (strict) validateStrictFetchOptions({
perspective,
stega
});
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 { result, resultSourceMap, syncTags } = await client.fetch(query, await params, {
filterResponse: false,
perspective,
variant,
stega,
returnQuery: false,
useCdn,
cacheMode,
tag: requestTag,
token
});
const tags = [...customCacheTags, ...(syncTags || []).map((tag) => `${cacheTagPrefix}${tag}`)];
/**
* The tags used here, are expired later on in the `action` Server Action given to `<SanityLive />` with the `revalidateTag` function from `next/cache`,
* or by a route handler that userland sets up.
*/
cacheTag(...tags);
/**
* Sanity Live handles on-demand revalidation, so the default 15min time-based revalidation is too short,
* userland can still set a shorter revalidate time by calling `cacheLife` themselves.
*/
cacheLife(sanity);
return {
data: result,
sourceMap: resultSourceMap || null,
tags
};
};
const SanityLive$2 = function SanityLive$1(props) {
if (strict) validateStrictSanityLiveProps(props);
const { includeDrafts: _includeDrafts = false, requestTag = "next-loader.live.cache-components", waitFor, action, onError, onWelcome, onReconnect, onRestart, onGoAway } = props;
const { projectId, dataset, apiHost, apiVersion, useProjectHostname, requestTagPrefix } = client.config();
const includeDrafts = typeof browserToken === "string" && !!browserToken && _includeDrafts;
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
};
}
export { defineLive, isCorsOriginError, parseTags, resolvePerspectiveFromCookies, resolveVariantFromCookies };
//# sourceMappingURL=index.js.map