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

SWR should throw when trying to suspend on the server #1832

Closed
kara opened this issue Feb 2, 2022 · 1 comment · Fixed by #1931
Closed

SWR should throw when trying to suspend on the server #1832

kara opened this issue Feb 2, 2022 · 1 comment · Fixed by #1931
Assignees

Comments

@kara
Copy link

kara commented Feb 2, 2022

Bug report

Description / Observed Behavior

Server-side streaming with useSWR appears to work, in that you can see the data fetched on the server and sent through script tags later on. However, it isn't currently set up to share data between the server and client, causing a client-side fetch to happen in addition to the server-side fetch. Since nothing breaks or warns, it looks like this is fully working when it's not.

Expected Behavior

As suggested by @devknoll here, useSWR should throw a friendly error when the the suspense flag is on and it tries to suspend on the server, so folks know this isn't yet implemented fully.

Repro Steps / Code Example

  1. Have the following code:
    index.js
<Suspense fallback={<div>Loading profile…</div>}>
    <Profile />
</Suspense>

profile.jsx

function Profile(props) {
   const data = useSWR(fetcher, '/some/profile', {suspense: true})
   return (
        <div> Profile ID: { data.id } </div>
   );
}

  1. Turn on the concurrentFeatures flag.
  2. Check the HTML response in the network tab and see the script tags being sent from the server with the data.
  3. See that there are also client-side fetches for the same data in the network tab.
@chungwu
Copy link

chungwu commented Dec 16, 2022

One use case that this breaks is when doing rendering in some "simulated" runtime on the server, like react-ssr-prepass, which depends on useSWR() throwing these promises so that it can wait for them to resolve and use the fetched data to pre-populate the data cache. Unfortunately, this now errors out with React 18 on the server. There is no "fallback" data to specify, since the point of using react-ssr-prepass is to populate that fallback data cache.

Would you consider a flag for SWR config that would preserve the previous behavior, throwing the promise even on React 18 + server?

Thanks!

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

Successfully merging a pull request may close this issue.

3 participants