-
-
Notifications
You must be signed in to change notification settings - Fork 79
Make Sanity Optional #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,23 @@ | ||
| import { NextResponse } from 'next/server' | ||
| import { defineEnableDraftMode } from 'next-sanity/draft-mode' | ||
| import { client } from '~/integrations/sanity/client' | ||
| import { privateToken } from '~/integrations/sanity/env' | ||
|
|
||
| export const { GET } = defineEnableDraftMode({ | ||
| client: client.withConfig({ token: privateToken }), | ||
| }) | ||
| function getEnableDraftMode() { | ||
| if (!(client && privateToken)) { | ||
| console.error('Sanity is not configured') | ||
| return { | ||
| GET: () => | ||
| NextResponse.json( | ||
| { error: 'Sanity is not configured' }, | ||
| { status: 500 } | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| return defineEnableDraftMode({ | ||
| client: client.withConfig({ token: privateToken }), | ||
| }) | ||
| } | ||
|
|
||
| export const { GET } = getEnableDraftMode() |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,36 @@ | ||
| import { createClient } from 'next-sanity' | ||
| import { isSanityConfigured } from '../check-integration' | ||
| import { apiVersion, dataset, privateToken, projectId, studioUrl } from './env' | ||
|
|
||
| export const client = createClient({ | ||
| projectId, | ||
| dataset, | ||
| apiVersion, | ||
| useCdn: true, | ||
| perspective: 'published', | ||
| token: privateToken, | ||
| stega: { | ||
| studioUrl, | ||
| filter: (props) => { | ||
| if (props.sourcePath.at(-1) === 'title') { | ||
| return true | ||
| } | ||
| let _client: ReturnType<typeof createClient> | null = null | ||
|
|
||
| return props.filterDefault(props) | ||
| }, | ||
| }, | ||
| }) | ||
| export function getClient() { | ||
| if (!isSanityConfigured()) { | ||
| console.error('Sanity is not configured') | ||
| return null | ||
| } | ||
|
|
||
| if (!_client) { | ||
| _client = createClient({ | ||
| projectId, | ||
| dataset, | ||
| apiVersion, | ||
| useCdn: true, | ||
| perspective: 'published', | ||
| token: privateToken, | ||
| stega: { | ||
| studioUrl, | ||
| filter: (props) => { | ||
| if (props.sourcePath.at(-1) === 'title') { | ||
| return true | ||
| } | ||
| return props.filterDefault(props) | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| return _client | ||
| } | ||
|
|
||
| export const client = getClient() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,37 @@ import { defineLive } from 'next-sanity/live' | |
| import { client } from '../client' | ||
| import { privateToken, publicToken } from '../env' | ||
|
|
||
| export const { sanityFetch, SanityLive } = defineLive({ | ||
| client, | ||
| browserToken: publicToken, | ||
| serverToken: privateToken, | ||
| }) | ||
| let _sanityLive: ReturnType<typeof defineLive> | null = null | ||
|
|
||
| function getSanityLive() { | ||
| if (!client) { | ||
| console.error('Sanity is not configured') | ||
| return null | ||
| } | ||
|
|
||
| if (!_sanityLive) { | ||
| _sanityLive = defineLive({ | ||
| client, | ||
| browserToken: publicToken, | ||
| serverToken: privateToken, | ||
|
Comment on lines
+16
to
+17
|
||
| }) | ||
| } | ||
|
|
||
| return _sanityLive | ||
| } | ||
|
|
||
| const sanityLiveInstance = getSanityLive() | ||
|
|
||
| type SanityLiveInstance = NonNullable<typeof sanityLiveInstance> | ||
| type SanityFetchFunction = SanityLiveInstance['sanityFetch'] | ||
|
|
||
| export const sanityFetch: SanityFetchFunction = ((...args) => { | ||
| if (!sanityLiveInstance?.sanityFetch) { | ||
| throw new Error( | ||
| 'Sanity is not configured. Please check your environment variables.' | ||
| ) | ||
| } | ||
| return sanityLiveInstance.sanityFetch(...args) | ||
| }) as SanityFetchFunction | ||
|
|
||
| export const SanityLive = sanityLiveInstance?.SanityLive || (() => null) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling is inconsistent across the codebase. Here you throw an error, but in
integrations/sanity/live/index.tsx:12-16you return a resolved promise with an error object, and inapp/api/draft-mode/enable/route.ts:11-15you return a 500 response.For consistency and better user experience, consider: