Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: slove the mutate not work problem (#10947)

Shinji-Li78ca5ebed54c
diff --git a/src/libs/swr/mutate.ts b/src/libs/swr/mutate.ts
index 7623143b79d..87e10333fd6 100644
--- a/src/libs/swr/mutate.ts
+++ b/src/libs/swr/mutate.ts
@@ -4,7 +4,7 @@
* When using a custom cache provider with SWRConfig, the global `mutate` from 'swr'
* becomes a no-op because it can't access the scoped cache.
*
- * This module provides a scoped mutate function that works with custom cache providers.
+ * This module stores the scoped mutate function from SWRConfig for use outside React components.
* The mutate function is initialized when the SWRConfig component mounts.
*
* @see https://github.com/vercel/swr/issues/2799
@@ -20,27 +20,37 @@
*/
import type { ScopedMutator } from 'swr/_internal';
-// Global scoped mutate reference, set when SWRConfig mounts
-let scopedMutate: ScopedMutator | null = null;
+// Mutable container to hold the scoped mutate reference
+// Using an object allows us to update the reference while keeping the same export
+const mutateRef: { current: ScopedMutator | null } = { current: null };
/**
* Set the scoped mutate function from SWRConfig
* Called internally by SWRProvider on mount
*/
export const setScopedMutate = (m: ScopedMutator) => {
- scopedMutate = m;
+ mutateRef.current = m;
};
/**
- * Scoped mutate function that works with custom cache providers
- *
- * Use this instead of `import { mutate } from 'swr'` when using localStorage cache provider
+ * Get the scoped mutate function
+ * Returns the actual mutate function from useSWRConfig(), not a wrapper
*/
-export const mutate: ScopedMutator = (async (key: any, data?: any, opts?: any) => {
- if (!scopedMutate) {
+export const getMutate = (): ScopedMutator => {
+ if (!mutateRef.current) {
console.warn('[SWR] Scoped mutate not initialized, this may cause cache sync issues');
- return [];
+ // Return a no-op function that returns empty array
+ return (() => []) as unknown as ScopedMutator;
}
+ return mutateRef.current;
+};
- return await scopedMutate(key, data, opts);
+/**
+ * Scoped mutate function that works with custom cache providers
+ * This is the actual mutate function from useSWRConfig(), stored globally
+ *
+ * Use this instead of `import { mutate } from 'swr'` when using localStorage cache provider
+ */
+export const mutate: ScopedMutator = (async (...args: Parameters<ScopedMutator>) => {
+ return await getMutate()(...args);
}) as ScopedMutator;