next-sanity
Version:
Sanity.io toolkit for Next.js
179 lines (177 loc) • 7.03 kB
TypeScript
import { ClientPerspective, ClientReturn, ContentSourceMap, LiveEventGoAway, QueryParams, SanityClient, SyncTag } from "@sanity/client";
//#region src/live/defineLive.d.ts
/**
* @public
*/
type DefinedSanityFetchType = <const QueryString extends string>(options: {
query: QueryString;
params?: QueryParams | Promise<QueryParams>;
/**
* Add custom `next.tags` to the underlying fetch request.
* @see https://nextjs.org/docs/app/api-reference/functions/fetch#optionsnexttags
* This can be used in conjunction with custom fallback revalidation strategies, as well as with custom Server Actions that mutate data and want to render with fresh data right away (faster than the Live Event latency).
* @defaultValue `['sanity']`
*/
tags?: string[];
perspective?: Exclude<ClientPerspective, "raw">;
stega?: boolean;
/**
* @deprecated use `requestTag` instead
*/
tag?: never;
/**
* This request tag is used to identify the request when viewing request logs from your Sanity Content Lake.
* @see https://www.sanity.io/docs/reference-api-request-tags
* @defaultValue 'next-loader.fetch'
*/
requestTag?: string;
}) => Promise<{
data: ClientReturn<QueryString>;
sourceMap: ContentSourceMap | null;
tags: string[];
}>;
/**
* @public
*/
type DefinedSanityLiveStreamType = <const QueryString extends string>(props: {
query: QueryString;
params?: QueryParams | Promise<QueryParams>;
/**
* Add custom `next.tags` to the underlying fetch request.
* @see https://nextjs.org/docs/app/api-reference/functions/fetch#optionsnexttags
* This can be used in conjunction with custom fallback revalidation strategies, as well as with custom Server Actions that mutate data and want to render with fresh data right away (faster than the Live Event latency).
* @defaultValue `['sanity']`
*/
tags?: string[];
perspective?: Exclude<ClientPerspective, "raw">;
stega?: boolean;
/**
* @deprecated use `requestTag` instead
*/
tag?: never;
/**
* This request tag is used to identify the request when viewing request logs from your Sanity Content Lake.
* @see https://www.sanity.io/docs/reference-api-request-tags
* @defaultValue 'next-loader.live-stream.fetch'
*/
requestTag?: string;
children: (result: {
data: ClientReturn<QueryString>;
sourceMap: ContentSourceMap | null;
tags: string[];
}) => Promise<Awaited<React.ReactNode>>;
}) => React.ReactNode;
/**
* @public
*/
interface DefinedSanityLiveProps {
/**
* Automatic refresh of RSC when the component <SanityLive /> is mounted.
* Note that this is different from revalidation, which is based on tags and causes `sanityFetch` calls to be re-fetched.
* @defaultValue `true`
*/
refreshOnMount?: boolean;
/**
* Automatically refresh when window gets focused
* Note that this is different from revalidation, which is based on tags and causes `sanityFetch` calls to be re-fetched.
* @defaultValue `false` if draftMode().isEnabled, otherwise `true` if not inside an iframe
*/
refreshOnFocus?: boolean;
/**
* Automatically refresh when the browser regains a network connection (via navigator.onLine)
* Note that this is different from revalidation, which is based on tags and causes `sanityFetch` calls to be re-fetched.
* @defaultValue `true`
*/
refreshOnReconnect?: boolean;
/**
* Automatically refresh on an interval when the Live Event API emits a `goaway` event, which indicates that the connection is rejected or closed.
* This typically happens if the connection limit is reached, or if the connection is idle for too long.
* To disable this long polling fallback behavior set `intervalOnGoAway` to `false` or `0`.
* You can also use `onGoAway` to handle the `goaway` event in your own way, and read the reason why the event was emitted.
* @defaultValue `30_000` 30 seconds interval
*/
intervalOnGoAway?: number | false;
/**
* @deprecated use `requestTag` instead
*/
tag?: never;
/**
* This request tag is used to identify the request when viewing request logs from your Sanity Content Lake.
* @see https://www.sanity.io/docs/reference-api-request-tags
* @defaultValue 'next-loader.live'
*/
requestTag?: string;
/**
* Handle errors from the Live Events subscription.
* By default it's reported using `console.error`, you can override this prop to handle it in your own way.
*/
onError?: (error: unknown) => void;
/**
* Handle the `goaway` event if the connection is rejected/closed.
* `event.reason` will be a string of why the event was emitted, for example `'connection limit reached'`.
* When this happens the `<SanityLive />` will fallback to long polling with a default interval of 30 seconds, providing your own `onGoAway` handler does not change this behavior.
* If you want to disable long polling set `intervalOnGoAway` to `false` or `0`.
*/
onGoAway?: (event: LiveEventGoAway, intervalOnGoAway: number | false) => void;
/**
* Override how cache tags are invalidated, you need to pass a server action here.
* You can also pass a `use client` function here, and have `router.refresh()` be called if the promise resolves to `'refresh'`.
*/
revalidateSyncTags?: (tags: SyncTag[]) => Promise<void | "refresh">;
}
/**
* @public
*/
interface DefineSanityLiveOptions {
/**
* Required for `sanityFetch` and `SanityLive` to work
*/
client: SanityClient;
/**
* Optional. If provided then the token needs to have permissions to query documents with `drafts.` prefixes in order for `perspective: 'drafts'` to work.
* This token is not shared with the browser.
*/
serverToken?: string | false;
/**
* Optional. This token is shared with the browser, and should only have access to query published documents.
* It is used to setup a `Live Draft Content` EventSource connection, and enables live previewing drafts stand-alone, outside of Presentation Tool.
*/
browserToken?: string | false;
/**
* Fetch options used by `sanityFetch`
*/
fetchOptions?: {
/**
* Optional, enables time based revalidation in addition to the EventSource connection.
* @defaultValue `false`
*/
revalidate?: number | false;
};
/**
* Optional. Include stega encoding when draft mode is enabled.
* @defaultValue `true`
*/
stega?: boolean;
}
/**
* @public
*/
declare function defineLive(config: DefineSanityLiveOptions): {
/**
* Use this function to fetch data from Sanity in your React Server Components.
* @public
*/
sanityFetch: DefinedSanityFetchType;
/**
* Render this in your root layout.tsx to make your page revalidate on new content live, automatically.
* @public
*/
SanityLive: React.ComponentType<DefinedSanityLiveProps>;
/**
* @alpha experimental, it may change or even be removed at any time
*/
SanityLiveStream: DefinedSanityLiveStreamType;
};
//#endregion
export { DefineSanityLiveOptions, DefinedSanityFetchType, DefinedSanityLiveProps, DefinedSanityLiveStreamType, defineLive };
//# sourceMappingURL=defineLive.d.ts.map