next-sanity
Version:
Sanity.io toolkit for Next.js
63 lines (62 loc) • 2.33 kB
JavaScript
"use server";
import { r as partitionedCookieName } from "../../constants.js";
import { n as sanitizePerspective, t as sanitizeVariant } from "../../sanitizeVariant.js";
import { perspectiveCookieName, variantCookieName } from "@sanity/preview-url-secret/constants";
import { cookies } from "next/headers";
import { refresh } from "next/cache";
/**
* @internal CAUTION: this is an internal action and does not follow semver. Using it directly is at your own risk.
*/
async function perspectiveChangeAction(perspective) {
const sanitizedPerspective = sanitizePerspective(perspective, "drafts");
if (!sanitizedPerspective || Array.isArray(sanitizedPerspective) && sanitizedPerspective.length === 0) throw new Error(`Invalid perspective`, { cause: perspective });
const nextPerspective = Array.isArray(sanitizedPerspective) ? sanitizedPerspective.join(",") : sanitizedPerspective;
const jar = await cookies();
if (nextPerspective === jar.get(perspectiveCookieName)?.value && process.env.NODE_ENV !== "production") {
console.debug("perspectiveChangeAction", "Perspective is the same, skipping", nextPerspective);
return;
}
jar.set(perspectiveCookieName, nextPerspective, {
httpOnly: true,
path: "/",
secure: true,
sameSite: "none",
partitioned: jar.has(partitionedCookieName)
});
refresh();
}
/**
* @internal CAUTION: this is an internal action and does not follow semver. Using it directly is at your own risk.
*/
async function variantChangeAction(variant) {
const sanitizedVariant = sanitizeVariant(variant);
const jar = await cookies();
const currentVariant = jar.get(variantCookieName)?.value;
if (!sanitizedVariant) {
if (!currentVariant) return;
jar.delete({
name: variantCookieName,
httpOnly: true,
path: "/",
secure: true,
sameSite: "none",
partitioned: jar.has(partitionedCookieName)
});
refresh();
return;
}
if (sanitizedVariant === currentVariant && process.env.NODE_ENV !== "production") {
console.debug("variantChangeAction", "Variant is the same, skipping", sanitizedVariant);
return;
}
jar.set(variantCookieName, sanitizedVariant, {
httpOnly: true,
path: "/",
secure: true,
sameSite: "none",
partitioned: jar.has(partitionedCookieName)
});
refresh();
}
export { perspectiveChangeAction, variantChangeAction };
//# sourceMappingURL=index.js.map