24 lines
689 B
TypeScript
24 lines
689 B
TypeScript
import {createClient, type SanityClient} from '@sanity/client';
|
|
|
|
let cached: SanityClient | null = null;
|
|
|
|
/**
|
|
* Server-only Sanity client with write access. Import only from loaders/actions
|
|
* and other `.server` modules — never from client components.
|
|
*/
|
|
export function getSanityWriteClient(env: Env): SanityClient {
|
|
const token = env.SANITY_API_WRITE_TOKEN;
|
|
if (!token) {
|
|
throw new Error('SANITY_API_WRITE_TOKEN is not configured');
|
|
}
|
|
if (cached) return cached;
|
|
cached = createClient({
|
|
projectId: env.SANITY_PROJECT_ID,
|
|
dataset: env.SANITY_DATASET,
|
|
apiVersion: env.SANITY_API_VERSION ?? '2026-04-16',
|
|
token,
|
|
useCdn: false,
|
|
});
|
|
return cached;
|
|
}
|