Created
July 18, 2021 18:37
-
-
Save j2is/5e4519586ff1774b22e3c90329f14f08 to your computer and use it in GitHub Desktop.
Next Sanity Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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