-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from demo-repositories/latest
Latest
- Loading branch information
Showing
22 changed files
with
2,240 additions
and
1,257 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import "server-only"; | ||
|
||
import * as queryStore from "@sanity/react-loader"; | ||
import { draftMode } from "next/headers"; | ||
|
||
import { client } from "@/lib/sanity/client"; | ||
|
||
import { token } from "@/lib/sanity/token"; | ||
|
||
const serverClient = client.withConfig({ | ||
token, | ||
stega: { | ||
// Enable stega if it's a Vercel preview deployment, as the Vercel Toolbar has controls that shows overlays | ||
enabled: process.env.VERCEL_ENV === "preview", | ||
}, | ||
}); | ||
|
||
/** | ||
* Sets the server client for the query store, doing it here ensures that all data fetching in production | ||
* happens on the server and not on the client. | ||
* Live mode in `sanity/presentation` still works, as it uses the `useLiveMode` hook to update `useQuery` instances with | ||
* live draft content using `postMessage`. | ||
*/ | ||
queryStore.setServerClient(serverClient); | ||
|
||
const usingCdn = serverClient.config().useCdn; | ||
// Automatically handle draft mode | ||
export const loadQuery = ((query, params = {}, options = {}) => { | ||
const { | ||
perspective = draftMode().isEnabled ? "previewDrafts" : "published", | ||
} = options; | ||
// Don't cache by default | ||
let revalidate: NextFetchRequestConfig["revalidate"] = 0; | ||
// If `next.tags` is set, and we're not using the CDN, then it's safe to cache | ||
if (!usingCdn && Array.isArray(options.next?.tags)) { | ||
revalidate = false; | ||
} else if (usingCdn) { | ||
revalidate = 60; | ||
} | ||
return queryStore.loadQuery(query, params, { | ||
...options, | ||
next: { | ||
revalidate, | ||
...(options.next || {}), | ||
}, | ||
perspective, | ||
// @TODO add support in `@sanity/client/stega` for the below | ||
// stega: {enabled: draftMode().isEnabled} | ||
}); | ||
}) satisfies typeof queryStore.loadQuery; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
type QueryParams, | ||
type QueryResponseInitial, | ||
type UseQueryOptionsDefinedInitial, | ||
} from "@sanity/react-loader"; | ||
import * as queryStore from "@sanity/react-loader"; | ||
|
||
/** | ||
* Exports to be used in client-only or components that render both server and client | ||
*/ | ||
export const useQuery = < | ||
QueryResponseResult = unknown, | ||
QueryResponseError = unknown, | ||
>( | ||
query: string, | ||
params?: QueryParams, | ||
options?: UseQueryOptionsDefinedInitial<QueryResponseResult>, | ||
) => { | ||
const snapshot = queryStore.useQuery<QueryResponseResult, QueryResponseError>( | ||
query, | ||
params, | ||
options, | ||
); | ||
|
||
// Always throw errors if there are any | ||
if (snapshot.error) { | ||
throw snapshot.error; | ||
} | ||
|
||
return snapshot; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import "server-only"; | ||
|
||
import { experimental_taintUniqueValue } from "react"; | ||
|
||
export const token = process.env.SANITY_API_READ_TOKEN; | ||
|
||
if (!token) { | ||
throw new Error("Missing SANITY_API_READ_TOKEN"); | ||
} | ||
|
||
experimental_taintUniqueValue( | ||
"Do not pass the sanity API read token to the client.", | ||
process, | ||
token, | ||
); |
Oops, something went wrong.