Skip to content

Commit

Permalink
fix(presentation): use live content API for loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 29, 2025
1 parent ca0adf9 commit 2cd609d
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 645 deletions.
6 changes: 0 additions & 6 deletions packages/sanity/package.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import {defineConfig} from '@sanity/pkg-utils'
export default defineConfig({
...baseConfig,

define: {
...baseConfig.define,
PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS:
process.env.PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS === 'true',
},

// Build unexposed bundles for scripts that need to be spawned/used in workers
bundles: [
{
Expand Down
1 change: 0 additions & 1 deletion packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@
"lodash": "^4.17.21",
"log-symbols": "^2.2.0",
"mendoza": "^3.0.0",
"mnemonist": "0.39.8",
"module-alias": "^2.2.2",
"nano-pubsub": "^3.0.0",
"nanoid": "^3.1.30",
Expand Down
31 changes: 10 additions & 21 deletions packages/sanity/src/presentation/PresentationTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {type RouterContextValue, useRouter} from 'sanity/router'
import {styled} from 'styled-components'
import {useEffectEvent} from 'use-effect-event'

import {DEFAULT_TOOL_NAME, EDIT_INTENT_MODE, LIVE_DRAFT_EVENTS_ENABLED} from './constants'
import {DEFAULT_TOOL_NAME, EDIT_INTENT_MODE} from './constants'
import PostMessageFeatures from './features/PostMessageFeatures'
import {debounce} from './lib/debounce'
import {SharedStateProvider} from './overlays/SharedStateProvider'
Expand Down Expand Up @@ -70,7 +70,6 @@ import {usePopups} from './usePopups'
import {usePreviewUrl} from './usePreviewUrl'
import {useStatus} from './useStatus'

const LoaderQueries = lazy(() => import('./loader/LoaderQueries'))
const LiveQueries = lazy(() => import('./loader/LiveQueries'))
const PostMessageDocuments = lazy(() => import('./overlays/PostMessageDocuments'))
const PostMessageRefreshMutations = lazy(() => import('./editor/PostMessageRefreshMutations'))
Expand Down Expand Up @@ -582,25 +581,15 @@ export default function PresentationTool(props: {
</PresentationNavigateProvider>
</PresentationProvider>
<Suspense>
{controller &&
(LIVE_DRAFT_EVENTS_ENABLED ? (
<LiveQueries
controller={controller}
perspective={perspective}
liveDocument={displayedDocument}
onDocumentsOnPage={setDocumentsOnPage}
onLoadersConnection={setLoadersConnection}
/>
) : (
<LoaderQueries
controller={controller}
perspective={perspective}
liveDocument={displayedDocument}
onDocumentsOnPage={setDocumentsOnPage}
onLoadersConnection={setLoadersConnection}
documentsOnPage={documentsOnPage}
/>
))}
{controller && (
<LiveQueries
controller={controller}
perspective={perspective}
liveDocument={displayedDocument}
onDocumentsOnPage={setDocumentsOnPage}
onLoadersConnection={setLoadersConnection}
/>
)}
{visualEditingComlink && params.id && params.type && (
<PostMessageRefreshMutations
comlink={visualEditingComlink}
Expand Down
16 changes: 0 additions & 16 deletions packages/sanity/src/presentation/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,5 @@ export const API_VERSION = '2023-10-16'
// Heartbeats shouldn't fire on intervals that are so short it causes performance issues
export const MIN_LOADER_QUERY_LISTEN_HEARTBEAT_INTERVAL = 1000 // ms

// Batch size for fetching documents building up the cache
export const LIVE_QUERY_CACHE_BATCH_SIZE = 100

// Total cache size for documents that are live queried
export const LIVE_QUERY_CACHE_SIZE = 2048

// The interval at which we check if existing popups have been closed
export const POPUP_CHECK_INTERVAL = 1000 // ms

declare global {
const PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS: unknown
}

// Feature flag that uses the new Live Draft Content API instead of the Listen API + Mendoza events
export const LIVE_DRAFT_EVENTS_ENABLED =
typeof PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS === 'string'
? PRESENTATION_ENABLE_LIVE_DRAFT_EVENTS === 'true'
: false
11 changes: 3 additions & 8 deletions packages/sanity/src/presentation/loader/LiveQueries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {
type ClientPerspective,
type ContentSourceMap,
createClient,
type LiveEventMessage,
type QueryParams,
type SyncTag,
Expand Down Expand Up @@ -43,7 +42,7 @@ import {
import {type DocumentOnPage} from '../useDocumentsOnPage'
import {mapChangedValue, useQueryParams, useRevalidate} from './utils'

export interface LoaderQueriesProps {
export interface LiveQueriesProps {
liveDocument: Partial<SanityDocument> | null | undefined
controller: Controller | undefined
perspective: ClientPerspective
Expand All @@ -55,7 +54,7 @@ export interface LoaderQueriesProps {
) => void
}

export default function LoaderQueries(props: LoaderQueriesProps): React.JSX.Element {
export default function LiveQueries(props: LiveQueriesProps): React.JSX.Element {
const {
liveDocument: _liveDocument,
controller,
Expand Down Expand Up @@ -216,11 +215,7 @@ export default function LoaderQueries(props: LoaderQueriesProps): React.JSX.Elem
}
})
useEffect(() => {
const liveClient = createClient(client.config()).withConfig({
// Necessary for the live drafts to work
apiVersion: 'vX',
})
const subscription = liveClient.live
const subscription = client.live
.events({includeDrafts: true, tag: 'presentation-loader'})
.subscribe({
next: (event) => {
Expand Down
Loading

0 comments on commit 2cd609d

Please sign in to comment.