···11-import type { App, Ref } from 'vue';
22-import { getCurrentInstance, inject, provide, isRef, shallowRef } from 'vue';
11+import { type App, getCurrentScope, type Ref } from 'vue';
22+import { inject, provide, isRef, shallowRef } from 'vue';
33import type { ClientOptions } from '@urql/core';
44import { Client } from '@urql/core';
5566-const clientsPerInstance = new WeakMap<{}, Ref<Client>>();
66+// WeakMap to store client instances as fallback when client is provided and used in the same component
77+const clientsPerScope = new WeakMap<{}, Ref<Client>>();
7888-/** Provides a {@link Client} to a component’s children.
99+/** Provides a {@link Client} to a component and it’s children.
910 *
1011 * @param opts - {@link ClientOptions}, a {@link Client}, or a reactive ref object of a `Client`.
1112 *
···1920 *
2021 * @example
2122 * ```ts
2222- * import { provideClient } from '@urql/vue';
2323- * // All of `@urql/core` is also re-exported by `@urql/vue`:
2424- * import { Client, cacheExchange, fetchExchange } from '@urql/core';
2323+ * <script setup>
2424+ * import { provideClient } from '@urql/vue';
2525+ * // All of `@urql/core` is also re-exported by `@urql/vue`:
2626+ * import { Client, cacheExchange, fetchExchange } from '@urql/core';
2527 *
2626- * export default {
2727- * setup() {
2828- * provideClient(new Client({
2929- * url: 'https://API',
3030- * exchanges: [cacheExchange, fetchExchange],
3131- * }));
3232- * },
3333- * };
2828+ * provideClient(new Client({
2929+ * url: 'https://API',
3030+ * exchanges: [cacheExchange, fetchExchange],
3131+ * }));
3232+ * </script>
3433 * ```
3534 */
3635export function provideClient(opts: ClientOptions | Client | Ref<Client>) {
···4140 client = opts;
4241 }
43424444- const instance = getCurrentInstance();
4545- if (instance) {
4646- clientsPerInstance.set(instance, client);
4343+ const scope = getCurrentScope();
4444+ if (scope) {
4545+ clientsPerScope.set(scope, client);
4746 }
48474948 provide('$urql', client);
···8887/** Returns a provided reactive ref object of a {@link Client}.
8988 *
9089 * @remarks
9191- * `useClient` may be called in Vue `setup` functions to retrieve a
9292- * reactive rev object of a {@link Client} that’s previously been
9090+ * `useClient` may be called in a reactive context to retrieve a
9191+ * reactive ref object of a {@link Client} that’s previously been
9392 * provided with {@link provideClient} in the current or a parent’s
9493 * `setup` function.
9594 *
9695 * @throws
9797- * In development, if `useClient` is called outside of a Vue `setup`
9898- * function or no {@link Client} was provided, an error will be thrown.
9696+ * In development, if `useClient` is called outside of a reactive context
9797+ * or no {@link Client} was provided, an error will be thrown.
9998 */
10099export function useClient(): Ref<Client> {
101101- const instance = getCurrentInstance();
102102- if (process.env.NODE_ENV !== 'production' && !instance) {
100100+ const scope = getCurrentScope();
101101+ if (process.env.NODE_ENV !== 'production' && !scope) {
103102 throw new Error(
104104- 'use* functions may only be called during the `setup()` or other lifecycle hooks.'
103103+ 'use* function must be called within a reactive context (component setup, composable, or effect scope).'
105104 );
106105 }
107106108107 let client = inject('$urql') as Ref<Client> | undefined;
109109- if (!client && instance) {
110110- client = clientsPerInstance.get(instance);
108108+ if (!client) {
109109+ client = clientsPerScope.get(scope!);
111110 }
112111113112 if (process.env.NODE_ENV !== 'production' && !client) {