next-sanity
Sanity.io toolkit for Next.js
Installing
$ npm install next-sanity
Usage
In some file, eg lib/sanity.js
:
import {
groq,
createClient,
createImageUrlBuilder,
createPreviewSubscriptionHook,
} from 'next-sanity'
const config = {
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
useCdn: process.env.NODE_ENV === 'production',
}
export const imageUrlBuilder = createImageUrlBuilder(config)
export const usePreviewSubscription = createPreviewSubscriptionHook(config)
export const sanityClient = createClient(config)
export const previewClient = createClient({
...config,
useCdn: false,
token: process.env.SANITY_API_TOKEN,
})
export const getClient = (usePreview) => (usePreview ? previewClient : sanityClient)
export const useCurrentUser = createCurrentUserHook(config)
In a page component, eg pages/posts/[slug].js
:
import ErrorPage from 'next/error'
import {useRouter} from 'next/router'
import {groq} from 'next-sanity'
import {getClient, usePreviewSubscription} from '../../lib/sanity'
const postQuery = groq`
*[_type == "post" && slug.current == $slug][0] {
_id,
title,
excerpt,
content,
coverImage,
"slug": slug.current
}
`
export default function Post({data, preview}) {
const router = useRouter()
if (!router.isFallback && !data.post?.slug) {
return <ErrorPage statusCode={404} />
}
const {data: post} = usePreviewSubscription(postQuery, {
params: {slug: data.post.slug},
initialData: data,
enabled: preview,
})
return (
<div>
<h1>{post.title}</h1>
<p>{post.excerpt}</p>
</div>
)
}
export async function getStaticProps({params, preview = false}) {
const post = await getClient(preview).fetch(postQuery, {
slug: params.slug,
})
return {
props: {
preview,
data: {post},
},
}
}
export async function getStaticPaths() {
const paths = await getClient().fetch(
groq`*[_type == "post" && defined(slug.current)][].slug.current`
)
return {
paths: paths.map((slug) => ({params: {slug}})),
fallback: true,
}
}
Documentation
@todo
License
MIT-licensed. See LICENSE.