Skip to content

Instantly share code, notes, and snippets.

@j2is
Created July 18, 2021 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j2is/dcc01c4a087fb02d70fa18120dd9e43d to your computer and use it in GitHub Desktop.
Save j2is/dcc01c4a087fb02d70fa18120dd9e43d to your computer and use it in GitHub Desktop.
Next Sanity Server
// lib/sanity.server.js
import { createClient } from "next-sanity";
import { config } from "./config";
// Set up the client for fetching data in the getProps page functions
export const sanityClient = createClient(config);
// Set up a preview client with serverless authentication for drafts
export const previewClient = createClient({
...config,
useCdn: false,
token: process.env.SANITY_API_TOKEN,
});
// Helper function for easily switching between normal client and preview client
export const getClient = (usePreview) => (usePreview ? previewClient : sanityClient);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment