From bbfe6c51cee157a82d35ae381c78fbb7464f2b71 Mon Sep 17 00:00:00 2001 From: Ryan Bonial Date: Thu, 4 Sep 2025 14:13:51 -0600 Subject: [PATCH] fix: build errors --- apps/dashboard/src/App.tsx | 28 +++++++++-------- apps/dashboard/vite.config.ts | 3 ++ apps/sdk-app/src/App.tsx | 24 ++++++++++----- packages/core/src/_exports/worker.ts | 45 ++++++++++++++++------------ 4 files changed, 61 insertions(+), 39 deletions(-) diff --git a/apps/dashboard/src/App.tsx b/apps/dashboard/src/App.tsx index 52b249140..cebf26d34 100644 --- a/apps/dashboard/src/App.tsx +++ b/apps/dashboard/src/App.tsx @@ -64,18 +64,18 @@ function SharedWorkerTest({iframeRef}: {iframeRef: React.RefObject { console.log('[Dashboard] Received query request:', data) - try { - // Type assert the data to the expected structure - const queryData = data as { - queryOptions: { - projectId: string - dataset: string - query: string - params?: Record - } - requestId: string + // Type assert the data to the expected structure + const queryData = data as { + queryOptions: { + projectId: string + dataset: string + query: string + params?: Record } + requestId: string + } + try { // Create a subscription request from the incoming query data const subscription = createSubscriptionRequest({ storeName: 'query', @@ -105,6 +105,7 @@ function SharedWorkerTest({iframeRef}: {iframeRef: React.RefObject handleStatus(workerStatus as WorkerStatus), heartbeat: false, // Disable heartbeat to reduce cycling onMessage: { 'dashboard/v1/query/request': handleQueryRequest, @@ -161,7 +162,10 @@ function SharedWorkerTest({iframeRef}: {iframeRef: React.RefObject {} + }, [connect, iframeRef]) const testSubscription = async () => { console.log('testSubscription') diff --git a/apps/dashboard/vite.config.ts b/apps/dashboard/vite.config.ts index e189914ef..07e044e62 100644 --- a/apps/dashboard/vite.config.ts +++ b/apps/dashboard/vite.config.ts @@ -32,6 +32,9 @@ export default defineConfig(({mode}) => { '@sanity/sdk-react': resolve(import.meta.dirname, '../../packages/react/src/_exports'), }, }, + worker: { + format: 'es', + }, define: { 'import.meta.env.VITE_IS_E2E': JSON.stringify(isE2E), 'import.meta.env.VITE_E2E_PROJECT_ID': JSON.stringify( diff --git a/apps/sdk-app/src/App.tsx b/apps/sdk-app/src/App.tsx index 098257fe1..fce4ab4e2 100644 --- a/apps/sdk-app/src/App.tsx +++ b/apps/sdk-app/src/App.tsx @@ -1,7 +1,7 @@ import {SanityApp, SanityConfig, useQuery, useWindowConnection} from '@sanity/sdk-react' import {Spinner, ThemeProvider} from '@sanity/ui' import {buildTheme} from '@sanity/ui/theme' -import {type JSX, useEffect, useRef, useState} from 'react' +import {type JSX} from 'react' const theme = buildTheme({}) @@ -25,7 +25,7 @@ type QueryRequestMessage = { type: 'dashboard/v1/query/request' data: { queryId: string - queryOptions: any + queryOptions: unknown requestId: string } } @@ -42,7 +42,6 @@ type QueryResponseMessage = { // Test component to demonstrate query forwarding function QueryTest() { - // hack -- something in the node setup in the query store has a race condition useWindowConnection({ name: 'sdk-app', @@ -50,12 +49,13 @@ function QueryTest() { }) // This query should be forwarded to Dashboard when in iframe context - const {data, isPending} = useQuery({ + const {data} = useQuery({ query: '*[_type == "movie"][0...5]{_id, title, releaseYear}', projectId: 'ppsg7ml5', dataset: 'test', }) + // eslint-disable-next-line no-console console.log('data', data) return ( @@ -66,7 +66,15 @@ function QueryTest() {
Data: -
+        
           {/* {JSON.stringify(data, null, 2)} */}
         
@@ -81,9 +89,9 @@ export default function App(): JSX.Element { return ( } config={devConfigs}> -
- -
+
+ +
) diff --git a/packages/core/src/_exports/worker.ts b/packages/core/src/_exports/worker.ts index 1be945ee0..f00650f88 100644 --- a/packages/core/src/_exports/worker.ts +++ b/packages/core/src/_exports/worker.ts @@ -7,17 +7,21 @@ */ import {createClient} from '@sanity/client' + import {sharedWorkerStore} from '../sharedWorkerStore/sharedWorkerStore' import {type SubscriptionRequest} from '../sharedWorkerStore/types' declare const self: SharedWorkerGlobalScope // Cache to store query results -const queryCache = new Map -}>() +const queryCache = new Map< + string, + { + result: unknown + timestamp: number + subscribers: Set + } +>() // Helper to create stable cache keys function getCacheKey(subscription: SubscriptionRequest): string { @@ -85,7 +89,7 @@ function handleRegisterSubscription(subscription: SubscriptionRequest, port: Mes try { // Register the subscription in the store sharedWorkerStore.getState().registerSubscription(subscription) - + // Check if we need to execute a query for this subscription if (subscription.storeName === 'query' && subscription.params?.['query']) { handleQuerySubscription(subscription, port) @@ -113,12 +117,15 @@ function handleRegisterSubscription(subscription: SubscriptionRequest, port: Mes * @internal * Handle query subscription - execute the query and cache results */ -async function handleQuerySubscription(subscription: SubscriptionRequest, port: MessagePort): Promise { +async function handleQuerySubscription( + subscription: SubscriptionRequest, + port: MessagePort, +): Promise { const cacheKey = getCacheKey(subscription) - + // Check if we already have this query result cached let cacheEntry = queryCache.get(cacheKey) - + if (!cacheEntry) { try { // Create Sanity client for this project/dataset @@ -128,22 +135,22 @@ async function handleQuerySubscription(subscription: SubscriptionRequest, port: apiVersion: '2024-01-01', useCdn: true, }) - + // Execute the query console.log('[SharedWorker] Executing query:', subscription.params?.['query']) const result = await client.fetch( subscription.params?.['query'] as string, - subscription.params?.['options'] as Record || {} + (subscription.params?.['options'] as Record) || {}, ) - + // Cache the result cacheEntry = { result, timestamp: Date.now(), - subscribers: new Set() + subscribers: new Set(), } queryCache.set(cacheKey, cacheEntry) - + console.log('[SharedWorker] Query executed and cached:', cacheKey) } catch (error) { console.error('[SharedWorker] Query execution failed:', error) @@ -151,16 +158,16 @@ async function handleQuerySubscription(subscription: SubscriptionRequest, port: type: 'SUBSCRIPTION_ERROR', data: { error: `Query execution failed: ${(error as Error).message}`, - subscriptionId: subscription.subscriptionId + subscriptionId: subscription.subscriptionId, }, }) return } } - + // Add this port as a subscriber to the cache entry cacheEntry.subscribers.add(port) - + // Send the query result back to the subscriber port.postMessage({ type: 'SUBSCRIPTION_REGISTERED', @@ -168,10 +175,10 @@ async function handleQuerySubscription(subscription: SubscriptionRequest, port: subscriptionId: subscription.subscriptionId, result: cacheEntry.result, cached: cacheEntry.timestamp !== Date.now(), - cacheKey + cacheKey, }, }) - + console.log('[SharedWorker] Query result sent to subscriber:', subscription.subscriptionId) }