Skip to content

Instantly share code, notes, and snippets.

@j2is
Created July 18, 2021 18:37
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/5e4519586ff1774b22e3c90329f14f08 to your computer and use it in GitHub Desktop.
Save j2is/5e4519586ff1774b22e3c90329f14f08 to your computer and use it in GitHub Desktop.
Next Sanity Client
// lib/sanity.js
import {
createImageUrlBuilder,
createPortableTextComponent,
createPreviewSubscriptionHook,
createCurrentUserHook,
} from "next-sanity";
import { config } from "./config";
if (!process.env.NEXT_PUBLIC_SANITY_PROJECT_ID) {
throw new Error("Couldn't find env var NEXT_PUBLIC_SANITY_PROJECT_ID!");
}
if (!process.env.NEXT_PUBLIC_SANITY_DATASET) {
throw new Error("Couldn't find env var NEXT_PUBLIC_SANITY_DATASET");
}
/**
* Set up a helper function for generating Image URLs with only the asset reference data in your documents.
* Read more: https://www.sanity.io/docs/image-url
**/
export const urlFor = (source) => createImageUrlBuilder(config).image(source);
// Set up the live preview subscription hook
export const usePreviewSubscription = createPreviewSubscriptionHook(config);
// Set up Portable Text serialization
export const PortableText = createPortableTextComponent({
...config,
// Serializers passed to @sanity/block-content-to-react
// (https://github.com/sanity-io/block-content-to-react)
serializers: {},
});
// Helper function for using the current logged in user account
export const useCurrentUser = createCurrentUserHook(config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment