@sanity/client 6.4.7 → 6.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,6 +58,7 @@ export async function updateDocumentTitle(_id, title) {
58
58
  - [ESM](#esm)
59
59
  - [CommonJS](#commonjs)
60
60
  - [TypeScript](#typescript)
61
+ - [Next.js App Router](#nextjs-app-router)
61
62
  - [Bun](#bun)
62
63
  - [Deno](#deno)
63
64
  - [Edge Runtime](#edge-runtime)
@@ -201,6 +202,43 @@ console.log(`Number of documents: ${data}`)
201
202
 
202
203
  Another alternative is [groqd].
203
204
 
205
+ #### [Next.js App Router](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-fetch)
206
+
207
+ ```tsx
208
+ import {createClient} from '@sanity/client'
209
+
210
+ const client = createClient({
211
+ projectId: 'your-project-id',
212
+ dataset: 'your-dataset-name',
213
+ useCdn: true, // set to `false` to bypass the edge cache
214
+ apiVersion: '2023-05-03', // use current date (YYYY-MM-DD) to target the latest API version
215
+ })
216
+
217
+ export default async function ReactServerComponent() {
218
+ const data = await client.fetch<number>(
219
+ `count(*[_type == "page"])`,
220
+ {},
221
+ {
222
+ // You can set any of the `cache` and `next` options as you would on a standard `fetch` call
223
+ cache: 'force-cache',
224
+ next: {tags: ['pages']},
225
+ },
226
+ )
227
+
228
+ return <h1>Number of pages: {data}</h1>
229
+ }
230
+ ```
231
+
232
+ The `cache` and `next` options are documented in the [Next.js documentation](https://nextjs.org/docs/app/api-reference/functions/fetch#fetchurl-options).
233
+ Since [request memoization](https://nextjs.org/docs/app/building-your-application/caching#request-memoization) is supported it's unnecessary to use [the `React.cache`](https://nextjs.org/docs/app/building-your-application/caching#react-cache-function) API.
234
+ To [opt-out of memoization](https://nextjs.org/docs/app/building-your-application/caching#opting-out), set the `signal` property:
235
+
236
+ ```tsx
237
+ const {signal} = new AbortController()
238
+ // By passing `signal` this request will not be memoized and `now()` will execute for every React Server Component that runs this query
239
+ const data = await client.fetch<number>(`{"dynamic": now()}`, {}, {signal})
240
+ ```
241
+
204
242
  #### [Bun]
205
243
 
206
244
  ```bash
@@ -225,6 +225,7 @@ once(function () {
225
225
  return console.warn(message.join(" "), ...args);
226
226
  });
227
227
  const printCdnWarning = createWarningPrinter(["Since you haven't set a value for `useCdn`, we will deliver content using our", "global, edge-cached API-CDN. If you wish to have content delivered faster, set", "`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."]);
228
+ const printCdnPreviewDraftsWarning = createWarningPrinter(["The Sanity client is configured with the `perspective` set to `previewDrafts`, which doesn't support the API-CDN.", "The Live API will be used instead. Set `useCdn: false` in your configuration to hide this warning."]);
228
229
  const printBrowserTokenWarning = createWarningPrinter(["You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.", "See ".concat(generateHelpUrl("js-client-browser-token"), " for more information and how to hide this warning.")]);
229
230
  const printNoApiVersionSpecifiedWarning = createWarningPrinter(["Using the Sanity client without specifying an API version is deprecated.", "See ".concat(generateHelpUrl("js-client-api-version"))]);
230
231
  const printNoDefaultExport = createWarningPrinter(["The default export of @sanity/client has been deprecated. Use the named export `createClient` instead."]);
@@ -1005,7 +1006,7 @@ function _requestObservable(client, httpRequest, options) {
1005
1006
  const uri = options.url || options.uri;
1006
1007
  const config = client.config();
1007
1008
  const canUseCdn = typeof options.canUseCdn === "undefined" ? ["GET", "HEAD"].indexOf(options.method || "GET") >= 0 && uri.indexOf("/data/") === 0 : options.canUseCdn;
1008
- const useCdn = config.useCdn && canUseCdn;
1009
+ let useCdn = config.useCdn && canUseCdn;
1009
1010
  const tag = options.tag && config.requestTagPrefix ? [config.requestTagPrefix, options.tag].join(".") : options.tag || config.requestTagPrefix;
1010
1011
  if (tag) {
1011
1012
  options.query = {
@@ -1027,6 +1028,10 @@ function _requestObservable(client, httpRequest, options) {
1027
1028
  perspective,
1028
1029
  ...options.query
1029
1030
  };
1031
+ if (perspective === "previewDrafts" && useCdn) {
1032
+ useCdn = false;
1033
+ printCdnPreviewDraftsWarning();
1034
+ }
1030
1035
  }
1031
1036
  }
1032
1037
  const reqOptions = requestOptions(config, Object.assign({}, options, {