swr icon indicating copy to clipboard operation
swr copied to clipboard

Make providing fallback data for `useSWRSubscription` more intuitive

Open NeutronicMC opened this issue 11 months ago • 1 comments

Bug report

Description / Observed Behavior

I was trying to provide fallback data to the useSWRSubscription hook, but no matter what I tried it would not use it. After looking through the source code, I realized that there is a constant SUBSCRIPTION_PREFIX that is prepended to any key provided. This isn't documented or clear to users of the library, and makes fallback data appear to not be available at all at first glance.

Expected Behavior

Make it clear in the documentation how normal SWR keys and subscription SWR keys are differentiated, and maybe provide a more ergonomic/future proof method of providing data to subscriptions. If useSWRSubscription is still experimental like the codebase comments say, it might be good to include this in the documentation as well.

Repro Steps / Code Example

useSWRSubscription<string>(
    "example",
    () => () => null,
    {
      fallback: {
        $sub$example: "this data is used",
        example: "this data is not used",
      },
    },
  );

Additional Context

SWR v2.3.2, Next.js v15.2.0-canary.56

For those of you in search of a quick fix: Append $sub$ to the beginning of each fallback key that needs to be accessed by a SWR subscription.

NeutronicMC avatar Feb 15 '25 04:02 NeutronicMC

It would at least be nice if this prefix was exported, somehow, or there was a function generate these names...

amadeus avatar Apr 08 '25 20:04 amadeus