|
| 1 | +import type { |
| 2 | + ClientPerspective, |
| 3 | + ClientReturn, |
| 4 | + ContentSourceMap, |
| 5 | + LiveEventGoAway, |
| 6 | + QueryParams, |
| 7 | + SanityClient, |
| 8 | +} from 'next-sanity' |
| 9 | + |
| 10 | +/** |
| 11 | + * Perspectives supported by Sanity Live. |
| 12 | + * Using the legacy `'raw'` perspective is not supported and leads to undefined behavior. |
| 13 | + */ |
| 14 | +export type PerspectiveType = Exclude<ClientPerspective, 'raw'> |
| 15 | + |
| 16 | +/** |
| 17 | + * TODO: docs |
| 18 | + */ |
| 19 | +export type DefinedFetchType = <const QueryString extends string>(options: { |
| 20 | + query: QueryString |
| 21 | + params?: QueryParams |
| 22 | + /** |
| 23 | + * @defaultValue 'published' |
| 24 | + */ |
| 25 | + perspective?: PerspectiveType |
| 26 | + /** |
| 27 | + * Enables stega encoding of the data, this is typically only used in draft mode in conjunction with `perspective: 'drafts'` and with `@sanity/visual-editing` setup. |
| 28 | + * @defaultValue `false` |
| 29 | + */ |
| 30 | + stega?: boolean |
| 31 | + /** |
| 32 | + * Custom cache tags that can be used with next's `updateTag` functions for custom `read-your-write` server actions, |
| 33 | + * for example a like button that uses client.mutate to update a document and then immediately shows the result. |
| 34 | + */ |
| 35 | + tags?: string[] |
| 36 | + /** |
| 37 | + * This request tag is used to identify the request when viewing request logs from your Sanity Content Lake. |
| 38 | + * @see https://www.sanity.io/docs/reference-api-request-tags |
| 39 | + * @defaultValue 'next-loader.fetch' |
| 40 | + */ |
| 41 | + requestTag?: string |
| 42 | +}) => Promise<{ |
| 43 | + data: ClientReturn<QueryString, unknown> |
| 44 | + sourceMap: ContentSourceMap | null |
| 45 | + tags: string[] |
| 46 | +}> |
| 47 | + |
| 48 | +export interface DefinedLiveProps { |
| 49 | + /** |
| 50 | + * TODO: should match the `perspective` you give `defineLive().fetch()`, setting it to a value other than `"published"` |
| 51 | + * and with `browserToken` set will cause it to subscribe to draft content changes as well as published content. |
| 52 | + */ |
| 53 | + perspective?: PerspectiveType |
| 54 | + |
| 55 | + /** |
| 56 | + * TODO: If Presentation Tool is present this event will fire with the current `perspective` stack used in the |
| 57 | + * Sanity Studio global perspective menu. The default event handler will store this state in a cookie, |
| 58 | + * which can be read with `resolvePerspectiveFromCookies` and used to ensure data fetching in the preview |
| 59 | + * matches the perspective and content viewed in the Studio, allowing you to quickly switch and preview different perspectives. |
| 60 | + */ |
| 61 | + onStudioPerspective?: (perspective: PerspectiveType) => void |
| 62 | + |
| 63 | + /** |
| 64 | + * Automatic refresh of RSC when the component <SanityLive /> is mounted. |
| 65 | + * @defaultValue `false` |
| 66 | + */ |
| 67 | + refreshOnMount?: boolean |
| 68 | + /** |
| 69 | + * Automatically refresh when window gets focused |
| 70 | + * @defaultValue `false` |
| 71 | + */ |
| 72 | + refreshOnFocus?: boolean |
| 73 | + /** |
| 74 | + * Automatically refresh when the browser regains a network connection (via navigator.onLine) |
| 75 | + * @defaultValue `false` |
| 76 | + */ |
| 77 | + refreshOnReconnect?: boolean |
| 78 | + /** |
| 79 | + * Automatically refresh on an interval when the Live Event API emits a `goaway` event, which indicates that the connection is rejected or closed. |
| 80 | + * This typically happens if the connection limit is reached, or if the connection is idle for too long. |
| 81 | + * To disable this long polling fallback behavior set `intervalOnGoAway` to `false` or `0`. |
| 82 | + * You can also use `onGoAway` to handle the `goaway` event in your own way, and read the reason why the event was emitted. |
| 83 | + * @defaultValue `30_000` 30 seconds interval |
| 84 | + */ |
| 85 | + intervalOnGoAway?: number | false |
| 86 | + |
| 87 | + /** |
| 88 | + * This request tag is used to identify the request when viewing request logs from your Sanity Content Lake. |
| 89 | + * @see https://www.sanity.io/docs/reference-api-request-tags |
| 90 | + * @defaultValue 'next-loader.live' |
| 91 | + */ |
| 92 | + requestTag?: string |
| 93 | + |
| 94 | + /** |
| 95 | + * Handle errors from the Live Events subscription. |
| 96 | + * By default it's reported using `console.error`, you can override this prop to handle it in your own way. |
| 97 | + */ |
| 98 | + onError?: (error: unknown) => void |
| 99 | + |
| 100 | + /** |
| 101 | + * Handle the `goaway` event if the connection is rejected/closed. |
| 102 | + * `event.reason` will be a string of why the event was emitted, for example `'connection limit reached'`. |
| 103 | + * 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. |
| 104 | + * If you want to disable long polling set `intervalOnGoAway` to `false` or `0`. |
| 105 | + */ |
| 106 | + onGoAway?: (event: LiveEventGoAway, intervalOnGoAway: number | false) => void |
| 107 | + |
| 108 | + /** |
| 109 | + * TODO: docs, this handles events for published content only, and can be used to revalidate content for all your users when in presentation tool |
| 110 | + */ |
| 111 | + onChange?: (tags: string[]) => Promise<void | 'refresh'> |
| 112 | + |
| 113 | + /** |
| 114 | + * TODO: docs, this handles events for all changes, published, drafts and even version documents in content releases. |
| 115 | + * It's only used when `browserToken` is provided, and the `perspective` prop is other than `"published"`. |
| 116 | + * Wether you should just `refresh()` or use `updateTag` to expire tags depends on how you fetch draft content and wether it's cached or not. |
| 117 | + */ |
| 118 | + onChangeIncludingDrafts?: (tags: string[]) => Promise<void | 'refresh'> |
| 119 | +} |
| 120 | + |
| 121 | +export interface LiveOptions { |
| 122 | + /** |
| 123 | + * Required for `fetch()` and `<Live>` to work |
| 124 | + */ |
| 125 | + client: SanityClient |
| 126 | + /** |
| 127 | + * Optional. If provided then the token needs to have permissions to query documents with `drafts.` prefixes in order for `perspective: 'drafts'` to work. |
| 128 | + * This token is never shared with the browser, unless you reuse it in `browserToken`.. |
| 129 | + */ |
| 130 | + serverToken?: string | false |
| 131 | + /** |
| 132 | + * Optional. This token is shared with the browser when `<Live>` is given a `perspective` prop other than `"published"`, and should only have access to query published documents. |
| 133 | + * It is used to setup a `Live Draft Content` EventSource connection, and enables live previewing drafts stand-alone, outside of Presentation Tool. |
| 134 | + */ |
| 135 | + browserToken?: string | false |
| 136 | +} |
0 commit comments