Skip to content

Commit d97d1f4

Browse files
committed
streamline exports
1 parent ddad769 commit d97d1f4

File tree

6 files changed

+151
-20
lines changed

6 files changed

+151
-20
lines changed

.changeset/twelve-moose-work.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"next-sanity": major
3+
---
4+
5+
Removed `type DefineSanityLiveOptions`, `type DefinedSanityFetchType` and `type DefinedSanityLiveProps` type exports

packages/next-sanity/src/cache-life.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* }
2727
*/
2828
export const sanity = {
29+
/**
30+
* Sanity Live handles on-demand revalidation, so the default 15min time based revalidation is too short
31+
*/
2932
revalidate: 7_776_000, // 90 days,
3033
} as const satisfies {
3134
/**

packages/next-sanity/src/experimental/live.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import SanityLiveClientComponent, {
1414
type SanityLiveProps,
1515
} from 'next-sanity/experimental/client-components/live'
16-
import {cacheTag, cacheLife, updateTag} from 'next/cache'
16+
import {cacheTag, updateTag} from 'next/cache'
1717
import {draftMode, cookies} from 'next/headers'
1818
import {preconnect} from 'react-dom'
1919

@@ -45,7 +45,7 @@ async function sanityCachedFetch<const QueryString extends string>(
4545
sourceMap: ContentSourceMap | null
4646
tags: string[]
4747
}> {
48-
'use cache: remote'
48+
// 'use cache: remote'
4949

5050
const client = createClient({...config, useCdn: true})
5151
const useCdn = perspective === 'published'
@@ -75,7 +75,7 @@ async function sanityCachedFetch<const QueryString extends string>(
7575
/**
7676
* Sanity Live handles on-demand revalidation, so the default 15min time based revalidation is too short
7777
*/
78-
cacheLife({revalidate: 60 * 60 * 24 * 90})
78+
// cacheLife({revalidate: 60 * 60 * 24 * 90})
7979

8080
return {data: result, sourceMap: resultSourceMap || null, tags}
8181
}
@@ -102,7 +102,8 @@ export interface SanityFetchOptions<QueryString extends string> {
102102
*/
103103
requestTag?: string
104104
/**
105-
* Custom cache tags that can be used with next's `revalidateTag` and `updateTag` functions for custom webhook on-demand revalidation.
105+
* Custom cache tags that can be used with next's `updateTag` functions for custom `read-your-write` server actions,
106+
* for example a like button that uses client.mutate to update a document and then immediately shows the result.
106107
*/
107108
tags?: string[]
108109
}

packages/next-sanity/src/live.next-js.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,5 @@
22
// Next.js will, when `cacheComponents: true`, automatically import `next-js` conditions instead of `react-server`, to allow targeting this mode.
33

44
export {isCorsOriginError} from '#live/isCorsOriginError'
5-
6-
export {
7-
type DefineSanityLiveOptions,
8-
type DefinedSanityFetchType,
9-
type DefinedSanityLiveProps,
10-
type SanityFetchOptions,
11-
defineLive,
12-
} from './experimental/live'
13-
5+
export {defineLive} from './experimental/live'
146
export {resolvePerspectiveFromCookies} from '#live/resolvePerspectiveFromCookies'

packages/next-sanity/src/live.react-server.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@
44
// Among other reasons we have to double-fetch to set cache tags, while the customer doesn't pay for the additional fetch it still adds latency to the server render.
55

66
export {isCorsOriginError} from '#live/isCorsOriginError'
7-
export {
8-
type DefineSanityLiveOptions,
9-
type DefinedSanityFetchType,
10-
type DefinedSanityLiveProps,
11-
defineLive,
12-
} from './live/defineLive'
13-
7+
export {defineLive} from './live/defineLive'
148
export {resolvePerspectiveFromCookies} from '#live/resolvePerspectiveFromCookies'
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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

Comments
 (0)