@@ -6,6 +6,12 @@ import { getClientKey } from "./utils";
66import { setupDevtools } from "./devtools/devtools" ;
77import { MaybeRefDeep } from "./types" ;
88
9+ declare global {
10+ interface Window {
11+ __VUE_QUERY_CONTEXT__ ?: QueryClient ;
12+ }
13+ }
14+
915export interface AdditionalClient {
1016 queryClient : QueryClient ;
1117 queryClientKey : string ;
@@ -15,12 +21,14 @@ interface ConfigOptions {
1521 queryClientConfig ?: MaybeRefDeep < QueryClientConfig > ;
1622 queryClientKey ?: string ;
1723 additionalClients ?: AdditionalClient [ ] ;
24+ contextSharing ?: boolean ;
1825}
1926
2027interface ClientOptions {
2128 queryClient ?: QueryClient ;
2229 queryClientKey ?: string ;
2330 additionalClients ?: AdditionalClient [ ] ;
31+ contextSharing ?: boolean ;
2432}
2533
2634export type VueQueryPluginOptions = ConfigOptions | ClientOptions ;
@@ -33,9 +41,24 @@ export const VueQueryPlugin = {
3341 if ( "queryClient" in options && options . queryClient ) {
3442 client = options . queryClient ;
3543 } else {
36- const clientConfig =
37- "queryClientConfig" in options ? options . queryClientConfig : undefined ;
38- client = new QueryClient ( clientConfig ) ;
44+ if ( options . contextSharing && typeof window !== "undefined" ) {
45+ if ( ! window . __VUE_QUERY_CONTEXT__ ) {
46+ const clientConfig =
47+ "queryClientConfig" in options
48+ ? options . queryClientConfig
49+ : undefined ;
50+ client = new QueryClient ( clientConfig ) ;
51+ window . __VUE_QUERY_CONTEXT__ = client ;
52+ } else {
53+ client = window . __VUE_QUERY_CONTEXT__ ;
54+ }
55+ } else {
56+ const clientConfig =
57+ "queryClientConfig" in options
58+ ? options . queryClientConfig
59+ : undefined ;
60+ client = new QueryClient ( clientConfig ) ;
61+ }
3962 }
4063
4164 client . mount ( ) ;
0 commit comments