vercel/swr · error · Error

The `subscribe` function must return a function to unsubscri

Error message

The `subscribe` function must return a function to unsubscribe.

What it means

Error "The `subscribe` function must return a function to unsubscribe." thrown in vercel/swr.

Source

Thrown at src/subscription/index.ts:77

      const next: SWRSubscriptionOptions<Data, Error>['next'] = (
        error,
        data
      ) => {
        if (error !== null && typeof error !== 'undefined') {
          set({ error })
        } else {
          set({ error: undefined })
          swr.mutate(data, false)
        }
      }

      // Increment the ref count.
      subscriptions.set(subscriptionKey, refCount + 1)

      if (!refCount) {
        const dispose = subscribe(args, { next })
        if (typeof dispose !== 'function') {
          throw new Error(
            'The `subscribe` function must return a function to unsubscribe.'
          )
        }
        disposers.set(subscriptionKey, dispose)
      }

      return () => {
        const count = subscriptions.get(subscriptionKey)! - 1

        subscriptions.set(subscriptionKey, count)

        // Dispose if it's the last one.
        if (!count) {
          const dispose = disposers.get(subscriptionKey)
          dispose?.()

          // Remove both entries, so the internal states only retain the
          // subscription keys that currently have active subscribers.

View on GitHub (pinned to 7173e55b2a)

When it happens

Trigger: Thrown at src/subscription/index.ts:77 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of vercel/swr@7173e55b2a (2026-08-27). Data as JSON: /api/errors/83545d5426b6f710. Report an issue: GitHub.