next-sanity
Version:
Sanity.io toolkit for Next.js
28 lines (27 loc) • 1.23 kB
JavaScript
"use server";
import { t as sanitizePerspective } from "../../sanitizePerspective.js";
import { perspectiveCookieName } 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"
});
refresh();
}
export { perspectiveChangeAction };
//# sourceMappingURL=index.js.map