Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useSWRSubscription suspends forever, never calls subscribe when suspense is true #2995

Open
steveluscher opened this issue Jul 13, 2024 · 1 comment

Comments

@steveluscher
Copy link

steveluscher commented Jul 13, 2024

Bug report

Description / Observed Behavior

When configured with { suspense: true }, useSWRSubscription will render repeatedly, suspend indefinitely, and never call subscribe().

Expected Behavior

I would expect useSWRSubscription to suspend until the first time next() is called with data.

Repro Steps / Code Example

function subscribe(_key, { next }) {
  console.log('calling subscribe()')
  let counter = 0;
  const intervalId = setInterval(() => {
    next(null /* err */, counter++ /* data */);
  }, 1000);
  return () => {
    clearInterval(intervalId);
  };
}

function Counter() {
  console.log("rendering Counter");
  const { data: count } = useSWRSubscription("count", subscribe, {
    suspense: true,
  });
  return <span>{count}</span>;
}

Codesandbox repro (WARNING: infinite render loop)

Additional Context

SWR 2.2.5.
Happens with strict mode and without.
When suspense is false then subscribe() gets called. You can try that out with the Codesandbox repro above.

This is almost certainly because this invocation of useSWRNext() with no fetcher pulls an infinite loop when suspense is true

https://github.com/vercel/swr/blob/main/src/subscription/index.ts#L38

@steveluscher
Copy link
Author

cc/ @huozhi @shuding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant