@@ -34,7 +34,11 @@ import {
3434import { type StoreState } from '../store/createStoreState'
3535import { defineStore , type StoreContext } from '../store/defineStore'
3636import { insecureRandomId } from '../utils/ids'
37- import { QUERY_STATE_CLEAR_DELAY , QUERY_STORE_API_VERSION } from './queryStoreConstants'
37+ import {
38+ QUERY_STATE_CLEAR_DELAY ,
39+ QUERY_STORE_API_VERSION ,
40+ QUERY_STORE_DEFAULT_PERSPECTIVE ,
41+ } from './queryStoreConstants'
3842import {
3943 addSubscriber ,
4044 cancelQuery ,
@@ -77,6 +81,28 @@ export const getQueryKey = (options: QueryOptions): string => JSON.stringify(opt
7781/** @beta */
7882export const parseQueryKey = ( key : string ) : QueryOptions => JSON . parse ( key )
7983
84+ /**
85+ * Ensures the query key includes an effective perspective so that
86+ * implicit differences (e.g. different instance.config.perspective)
87+ * don't collide in the dataset-scoped store.
88+ *
89+ * Since perspectives are unique, we can depend on the release stacks
90+ * to be correct when we retrieve the results.
91+ *
92+ */
93+ function normalizeOptionsWithPerspective (
94+ instance : SanityInstance ,
95+ options : QueryOptions ,
96+ ) : QueryOptions {
97+ if ( options . perspective !== undefined ) return options
98+ const instancePerspective = instance . config . perspective
99+ return {
100+ ...options ,
101+ perspective :
102+ instancePerspective !== undefined ? instancePerspective : QUERY_STORE_DEFAULT_PERSPECTIVE ,
103+ }
104+ }
105+
80106const queryStore = defineStore < QueryStoreState > ( {
81107 name : 'QueryStore' ,
82108 getInitialState : ( ) => ( { queries : { } } ) ,
@@ -255,16 +281,16 @@ export function getQueryState(
255281const _getQueryState = bindActionByDataset (
256282 queryStore ,
257283 createStateSourceAction ( {
258- selector : ( { state} : SelectorContext < QueryStoreState > , options : QueryOptions ) => {
284+ selector : ( { state, instance } : SelectorContext < QueryStoreState > , options : QueryOptions ) => {
259285 if ( state . error ) throw state . error
260- const key = getQueryKey ( options )
286+ const key = getQueryKey ( normalizeOptionsWithPerspective ( instance , options ) )
261287 const queryState = state . queries [ key ]
262288 if ( queryState ?. error ) throw queryState . error
263289 return queryState ?. result
264290 } ,
265- onSubscribe : ( { state} , options : QueryOptions ) => {
291+ onSubscribe : ( { state, instance } , options : QueryOptions ) => {
266292 const subscriptionId = insecureRandomId ( )
267- const key = getQueryKey ( options )
293+ const key = getQueryKey ( normalizeOptionsWithPerspective ( instance , options ) )
268294
269295 state . set ( 'addSubscriber' , addSubscriber ( key , subscriptionId ) )
270296
@@ -314,8 +340,9 @@ export function resolveQuery(...args: Parameters<typeof _resolveQuery>): Promise
314340const _resolveQuery = bindActionByDataset (
315341 queryStore ,
316342 ( { state, instance} , { signal, ...options } : ResolveQueryOptions ) => {
317- const { getCurrent} = getQueryState ( instance , options )
318- const key = getQueryKey ( options )
343+ const normalized = normalizeOptionsWithPerspective ( instance , options )
344+ const { getCurrent} = getQueryState ( instance , normalized )
345+ const key = getQueryKey ( normalized )
319346
320347 const aborted$ = signal
321348 ? new Observable < void > ( ( observer ) => {
0 commit comments