Skip to content

Commit 522d696

Browse files
committed
Fix missing server action
1 parent 1907db8 commit 522d696

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

.changeset/free-hoops-marry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"next-sanity": patch
3+
---
4+
5+
Fix missing server action

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {useEffect, useMemo, useRef, useState, useEffectEvent} from 'react'
1818
import type {SanityClientConfig} from '../types'
1919

2020
import {PUBLISHED_SYNC_TAG_PREFIX, type DRAFT_SYNC_TAG_PREFIX} from '../constants'
21+
import { expireTags } from 'next-sanity/live/server-actions'
2122

2223
const PresentationComlink = dynamic(() => import('./PresentationComlink'), {ssr: false})
2324
const RefreshOnMount = dynamic(() => import('../../live/client-components/live/RefreshOnMount'), {
@@ -49,7 +50,7 @@ export interface SanityLiveProps {
4950
intervalOnGoAway?: number | false
5051
onGoAway?: (event: LiveEventGoAway, intervalOnGoAway: number | false) => void
5152
revalidateSyncTags: (
52-
tags: `${typeof PUBLISHED_SYNC_TAG_PREFIX | typeof DRAFT_SYNC_TAG_PREFIX}${SyncTag}`[],
53+
tags: `${typeof PUBLISHED_SYNC_TAG_PREFIX | typeof DRAFT_SYNC_TAG_PREFIX}${string}`[],
5354
) => Promise<void | 'refresh'>
5455
resolveDraftModePerspective: () => Promise<ClientPerspective>
5556
}
@@ -100,7 +101,7 @@ export default function SanityLive(props: SanityLiveProps): React.JSX.Element |
100101
requestTag = 'next-loader.live',
101102
onError = handleError,
102103
onGoAway = handleOnGoAway,
103-
revalidateSyncTags,
104+
revalidateSyncTags = expireTags,
104105
resolveDraftModePerspective,
105106
} = props
106107
const {projectId, dataset, apiHost, apiVersion, useProjectHostname, token, requestTagPrefix} =

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ export function defineLive(config: LiveOptions): {
126126
intervalOnGoAway,
127127
} = props
128128

129-
if (onChange) {
130-
console.warn('`onChange` is not implemented yet')
131-
}
129+
132130
if (onChangeIncludingDrafts) {
133131
console.warn('`onChangeIncludingDrafts` is not implemented yet')
134132
}
@@ -166,7 +164,7 @@ export function defineLive(config: LiveOptions): {
166164
onError={onError}
167165
onGoAway={onGoAway}
168166
intervalOnGoAway={intervalOnGoAway}
169-
revalidateSyncTags={expireTags}
167+
revalidateSyncTags={onChange}
170168
resolveDraftModePerspective={resolveDraftModePerspective}
171169
/>
172170
</Suspense>
@@ -189,31 +187,7 @@ export function defineLive(config: LiveOptions): {
189187
}
190188
}
191189

192-
// @TODO expose parseTags function that returns the correct array of tags
193-
// we already have s1: prefixes, but they could change
194-
// use sp: for prod, sd: for draft, keep em short
195-
async function expireTags(_tags: unknown): Promise<void> {
196-
'use server'
197-
// @TODO Draft Mode bypasses cache anyway so we don't bother with expiring tags for draft content
198-
// const isDraftMode = (await draftMode()).isEnabled
199-
// const tags = _tags.map((tag) => `${isDraftMode ? 'drafts' : 'sanity'}:${tag}`)
200-
if (!Array.isArray(_tags)) {
201-
console.warn('<SanityLive /> `expireTags` called with non-array tags', _tags)
202-
return undefined
203-
}
204-
const tags = _tags.filter(
205-
(tag) => typeof tag === 'string' && tag.startsWith(PUBLISHED_SYNC_TAG_PREFIX),
206-
)
207-
if (!tags.length) {
208-
console.warn('<SanityLive /> `expireTags` called with no valid tags', _tags)
209-
return undefined
210-
}
211-
for (const tag of tags) {
212-
updateTag(tag)
213-
}
214-
// oxlint-disable-next-line no-console
215-
console.log(`<SanityLive /> updated tags: ${tags.join(', ')}`)
216-
}
190+
217191

218192
async function resolveDraftModePerspective(): Promise<PerspectiveType> {
219193
'use server'

packages/next-sanity/src/live/server-actions/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import type {ClientPerspective, SyncTag} from '@sanity/client'
44

55
import {sanitizePerspective} from '#live/sanitizePerspective'
66
import {perspectiveCookieName} from '@sanity/preview-url-secret/constants'
7-
import {revalidateTag} from 'next/cache'
7+
import {revalidateTag, updateTag} from 'next/cache'
88
import {cookies, draftMode} from 'next/headers'
9+
import { PUBLISHED_SYNC_TAG_PREFIX } from '../../experimental/constants'
910

1011
export async function revalidateSyncTags(tags: SyncTag[]): Promise<void> {
1112
revalidateTag('sanity:fetch-sync-tags', 'max')
@@ -39,3 +40,30 @@ export async function setPerspectiveCookie(perspective: ClientPerspective): Prom
3940
},
4041
)
4142
}
43+
44+
45+
// @TODO expose parseTags function that returns the correct array of tags
46+
// we already have s1: prefixes, but they could change
47+
// use sp: for prod, sd: for draft, keep em short
48+
export async function expireTags(_tags: unknown): Promise<void> {
49+
50+
// @TODO Draft Mode bypasses cache anyway so we don't bother with expiring tags for draft content
51+
// const isDraftMode = (await draftMode()).isEnabled
52+
// const tags = _tags.map((tag) => `${isDraftMode ? 'drafts' : 'sanity'}:${tag}`)
53+
if (!Array.isArray(_tags)) {
54+
console.warn('<SanityLive /> `expireTags` called with non-array tags', _tags)
55+
return undefined
56+
}
57+
const tags = _tags.filter(
58+
(tag) => typeof tag === 'string' && tag.startsWith(PUBLISHED_SYNC_TAG_PREFIX),
59+
)
60+
if (!tags.length) {
61+
console.warn('<SanityLive /> `expireTags` called with no valid tags', _tags)
62+
return undefined
63+
}
64+
for (const tag of tags) {
65+
updateTag(tag)
66+
}
67+
// oxlint-disable-next-line no-console
68+
console.log(`<SanityLive /> updated tags: ${tags.join(', ')}`)
69+
}

0 commit comments

Comments
 (0)