diff --git a/packages/sanity/src/core/preview/availability.ts b/packages/sanity/src/core/preview/availability.ts index cb5bfaf0218..f79ce5cb254 100644 --- a/packages/sanity/src/core/preview/availability.ts +++ b/packages/sanity/src/core/preview/availability.ts @@ -6,7 +6,7 @@ import {combineLatest, defer, from, type Observable, of} from 'rxjs' import {distinctUntilChanged, map, mergeMap, reduce, switchMap} from 'rxjs/operators' import shallowEquals from 'shallow-equals' -import {createSWR, getDraftId, getPublishedId, getVersionId, isRecord, isVersionId} from '../util' +import {createSWR, getDraftId, getPublishedId, getVersionId, isRecord} from '../util' import { AVAILABILITY_NOT_FOUND, AVAILABILITY_PERMISSION_DENIED, @@ -150,7 +150,7 @@ export function createPreviewAvailabilityObserver( ): Observable { const draftId = getDraftId(id) const publishedId = getPublishedId(id) - const versionId = isVersionId(id) && version ? getVersionId(id, version) : undefined + const versionId = version ? getVersionId(id, version) : undefined return combineLatest([ observeDocumentAvailability(draftId), observeDocumentAvailability(publishedId), diff --git a/packages/sanity/src/core/validation/validateDocumentWithReferences.ts b/packages/sanity/src/core/validation/validateDocumentWithReferences.ts index 2b52c2ab5e8..5ebb2f3fb99 100644 --- a/packages/sanity/src/core/validation/validateDocumentWithReferences.ts +++ b/packages/sanity/src/core/validation/validateDocumentWithReferences.ts @@ -29,11 +29,17 @@ import { shareReplay, skip, throttleTime, + withLatestFrom, } from 'rxjs/operators' import {exhaustMapWithTrailing} from 'rxjs-exhaustmap-with-trailing' import shallowEquals from 'shallow-equals' -import {type DocumentPreviewStore, type LocaleSource, type SourceClientOptions} from '..' +import { + type DocumentPreviewStore, + getVersionFromId, + type LocaleSource, + type SourceClientOptions, +} from '..' import {validateDocumentObservable} from './validateDocument' /** @@ -70,8 +76,11 @@ type GetDocumentExists = NonNullable const listenDocumentExists = ( observeDocumentAvailability: DocumentPreviewStore['unstable_observeDocumentPairAvailability'], id: string, + versionId: string | undefined, ): Observable => - observeDocumentAvailability(id).pipe(map(({published}) => published.available)) + observeDocumentAvailability(id, {version: versionId}).pipe( + map(({published, version}) => published.available || version?.available || false), + ) // throttle delay for referenced document updates (i.e. time between responding to changes in referenced documents) const REF_UPDATE_DELAY = 1000 @@ -98,14 +107,19 @@ export function validateDocumentWithReferences( mergeMap((ids) => from(ids)), ) + const versionId$ = document$.pipe( + map((doc) => getVersionFromId(doc?._id || '')), + distinctUntilChanged(), + ) // Note: we only use this to trigger a re-run of validation when a referenced document is published/unpublished const referenceExistence$ = referenceIds$.pipe( groupBy((id) => id, {duration: () => timer(1000 * 60 * 30)}), mergeMap((id$) => id$.pipe( distinct(), - mergeMap((id) => - listenDocumentExists(ctx.observeDocumentPairAvailability, id).pipe( + withLatestFrom(versionId$), + mergeMap(([id, versionId]) => + listenDocumentExists(ctx.observeDocumentPairAvailability, id, versionId).pipe( map( // eslint-disable-next-line max-nested-callbacks (result) => [id, result] as const,