diff --git a/_internal/src/utils/serialize.ts b/_internal/src/utils/serialize.ts index 6feda8542..8b3ad128e 100644 --- a/_internal/src/utils/serialize.ts +++ b/_internal/src/utils/serialize.ts @@ -1,9 +1,9 @@ import { stableHash } from './hash' import { isFunction } from './shared' -import type { Key } from '../types' +import type { Key, Arguments } from '../types' -export const serialize = (key: Key): [string, Key] => { +export const serialize = (key: Key): [string, Arguments] => { if (isFunction(key)) { try { key = key() diff --git a/subscription/src/index.ts b/subscription/src/index.ts index b01552c9a..3cf77bb6d 100644 --- a/subscription/src/index.ts +++ b/subscription/src/index.ts @@ -25,8 +25,7 @@ export const subscription = ((useSWRNext: SWRHook) => subscribe: SWRSubscription, config: SWRConfiguration & typeof SWRConfig.defaultValue ): SWRSubscriptionResponse => { - const [key] = serialize(_key) - const originKey = _key + const [key, args] = serialize(_key) // Prefix the key to avoid conflicts with other SWR resources. const subscriptionKey = key ? SUBSCRIPTION_PREFIX + key : undefined @@ -68,7 +67,7 @@ export const subscription = ((useSWRNext: SWRHook) => subscriptions.set(subscriptionKey, refCount + 1) if (!refCount) { - const dispose = subscribe(originKey, { next }) + const dispose = subscribe(args, { next }) if (typeof dispose !== 'function') { throw new Error( 'The `subscribe` function must return a function to unsubscribe.' diff --git a/test/use-swr-subscription.test.tsx b/test/use-swr-subscription.test.tsx index 3a7fb2bdd..50bf73bf6 100644 --- a/test/use-swr-subscription.test.tsx +++ b/test/use-swr-subscription.test.tsx @@ -82,7 +82,7 @@ describe('useSWRSubscription', () => { } function Page() { - const { data, error } = useSWRSubscription([swrKey], subscribe, { + const { data, error } = useSWRSubscription(() => [swrKey], subscribe, { fallbackData: 'fallback' }) return (