storybook: [Bug]: Infinite re-render for async RSC stories

Describe the bug

There was always some funkiness around using await in RSC story renders in Storybook (eg see https://github.com/amannn/next-intl/discussions/771#discussioncomment-10610137), but in the version 8.5.0+ the behavior has changed for the worse.

Now if I have a server component with await in it, some recent change on the Storybook side seems to be causing an infinite render. It’s a bit tricky to point out what factor exactly causes this, as in my isolated repro even a single await is enough to trigger it, whereas in my actual codebase it only seems to happen when there are 2+.

Image

Reproduction link

https://github.com/dmitrc/storybook-repro

Reproduction steps

git clone https://github.com/dmitrc/storybook-repro
cd storybook-repro
pnpm install
pnpm dev # verify that the components render
pnpm storybook # open the console to see the infinite render loop

Or setup from scratch:

pnpm dlx create-next-app@latest
pnpm dlx storybook@latest init

.storybook/main.ts:

  features: {
    experimentalRSC: true,
    developmentModeForBuild: true,
  },

page.tsx:

export default function Home() {
  return (
    <div>
      <ServerComponentWithOneAwait />
      <ServerComponentWithManyAwaits />
    </div>
  );
}

export async function ServerComponentWithOneAwait() {
  console.log(`ServerComponentWithOneAwait`);
  const md1 = await getMockData(1);
  return <p>{md1}</p>
}

export async function ServerComponentWithManyAwaits() {
  console.log(`ServerComponentWithManyAwaits`);
  const md1 = await getMockData(1);
  const md2 = await getMockData(2);
  return <p>{md1} + {md2}</p>
}

export function getMockData(i: number): Promise<string> {
  return new Promise((resolve) => setTimeout(() => resolve(`mock data ${i}`), 1000));
}

ServerComponentWithOneAwait.stories.tsx:

import type { Meta, StoryObj } from "@storybook/react";
import { ServerComponentWithOneAwait } from "../app/page";

const meta: Meta<typeof ServerComponentWithOneAwait> = {
  title: "RSC/OneAwait",
  component: ServerComponentWithOneAwait,
};

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {};

System

Storybook Environment Info:

  System:
    OS: Linux 5.15 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (32) x64 AMD Ryzen Threadripper PRO 3955WX 16-Cores
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 22.12.0 - ~/.nvm/versions/node/v22.12.0/bin/node
    npm: 10.9.0 - ~/.nvm/versions/node/v22.12.0/bin/npm
    pnpm: 9.15.1 - ~/.nvm/versions/node/v22.12.0/bin/pnpm <----- active
  npmPackages:
    @storybook/nextjs: ^8.5.0 => 8.5.0
    @storybook/react: ^8.5.0 => 8.5.0
    eslint-plugin-storybook: ^0.11.2 => 0.11.2
    storybook: ^8.5.0 => 8.5.0

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 15
  • Comments: 40 (12 by maintainers)

Most upvoted comments

🚨Update for everybody blocked on this issue🚨

The team looked into short-term solutions for RSC that fit into our current architecture, but it appears that to properly solve this we will need to render RSC on the server. We prototyped a server-side solution a year ago before the Suspense hack was surfaced by the community. However, the server-based approach is a huge departure from most of the rest of Storybook, which was why we shelved it as soon as Suspense seemed viable.

Now we’re at a crossroads. I’m discussing with the team whether we want to revive the server-side approach and how we might go about releasing it. It’s a major commitment for Storybook, so it’s not something we’d take on casually. I will also reach out to the React core team to see whether there are other possible solutions that we overlooked or changes on the RSC side that would help us out. This issue also seems to be affecting React Testing Library as well: https://github.com/testing-library/react-testing-library/issues/1209

FYI, I met with @mattcarrollcode to discuss possible solutions. He’s (unsurprisingly) of the opinion that rendering the components on a server is the “proper” long-term solution. We also discussed the need for a short-term solution, and plan to explore ideas together. The Storybook team is busy shipping Storybook 9, which coming in late May. I will continue the conversation between now and then, and then try to create space for shipping a solution in 9.x. Thanks everybody for bearing with us during this challenge!

Here’s an experiment that @valentinpalkovic and I are playing around with. I’m not super optimistic about this direction, but we’re trying to get creative about supporting RSCs in our current architecture. I’ll try to create an experimental release for feedback once it’s a little more mature!

We found out that what we need is 2 different bundles (a RSC and client bundle), but not necessarily run those one of those bundles on the server.

The API that transforms RSC into react flight chunks is also available in the browser. I made a POC for that here for the people interested: https://github.com/kasperpeulen/rsc-webpack-browser

It uses:

  • react-server-dom-webpack/plugin for the client bundle and manifest
  • react-server-dom-webpack/server for creating React Flight chunks
  • react-server-dom-webpack/client for rendering flight chunks to JSX
  • react-server-dom-webpack/node-loader for changing client components to references in server components
  • createFromReadableStream > callServer for hooking in server actions

Not needing to run RSC on an actual server makes everything much more in line with the storybook mdel. If we had to run the stories file on an actual server, users would lose the ability to set browser mocks (such as MSW) in a beforeEach or run a play function in their CSF file.

@kasperpeulen another benefit of running Storybook without a server is reliability and maintainability. When it’s a static site, it’s much more easier to upload it on S3 and forget about it. Putting a server in the mix would add a ton of friction.

Fingers crossed that the server-less model works out! 🤞

This sample was created from an idea, but it can render some Async Component.

The problem is the case where the parent is also a Server Component and the Async Component is used as an inner component. In this case, the Component seen from the outside is not an Async Component, so Promise cannot be taken out.

const sleep = (msec: number) => new Promise(resolve => setTimeout(resolve, msec))

const InsideComponent = async () => {
  await sleep(100)
  return (
    <p>inside</p>
  )
}

export const OutsideComponent = () => {
  return (
    <div>
      <InsideComponent /> {/*<- failed to render this component*/}
    </div>
  )
}

Any more than this may require intervention in the internal renderer rather than the decorator

https://gist.github.com/NEKOYASAN/f6e192194169bb9f22e8cb2ef2c84eca

@boylec thanks for all your help getting us up to speed on the problem!

I just downgraded Next.js to 15.0.4 and Storybook to 8.4.7 and it works, thx @kawamt .

I’m gonna wait for a better solution, thankfully my project doesn’t rely on last updates of the framework and the library.

renderToReadableStream & hydrateRoot approach trying in #30419. This approach seems to work fine for now, but there may be some problems or potential bugs.

Thank you for your advice @NEKOYASAN

Although it’s a bit of a workaround, I avoided the issue by creating a package.json in a subdirectory and fixing the Next.js version that Storybook internally depends on to v15.0.4

.storybook/main.js
sb/package.json // Next.js v15.0.4
package.json // Next.js v15.1.6
// sb/package.json
{
  "name": "subdirectory storybook",
  "scripts": {
    "storybook": "storybook dev --config-dir ../.storybook"
  },
  "devDependencies": {
    "next": "15.0.4",
    "storybook": "8.4.7"
  }
}
// package.json
{
  "name": "project",
  "scripts": {
    "storybook": "yarn --cwd sb storybook"
  },
  "devDependencies": {
    "next": "15.1.6",
    "storybook": "8.4.7"
    ...
  }
}

Since the Next.js code used within Storybook depends on the packages installed in the runtime environment, this solution works. It’s not ideal, but I wanted a quick solution since Next.js v15.1.2 includes security updates.

You’re probably right @boylec. We are booked up through 9.0, but I will discuss with the team to try to get it in as soon as we can manage it. If anybody wants to flex their RSC muscles and try to give this a shot, we are more than happy to advise.

See my comment there on #30033

Here is the diff between next.js v15.0.4-canary.26 and v15.0.4-canary.27

In my environment, I’ve isolated the regression behavior in storybook to this change. In other words when I revert my Next.JS version to v15.0.4-canary.26 it works. When I upgrade to v15.0.4-canary.27 it starts failing as the OP described.

This Next.JS diff includes a React upgrade from 19.0.0-rc-380f5d67-20241113 to 19.0.0-rc-b01722d5-20241114.

Here is the diff for those react versions which represents going from React RC 0 to React RC 1.

Here is a post about React 19 RC 1 from the React team.

It seems the big difference is that the React 19 enableSiblingPrerendering feature flag is switched to true. Not sure yet how or why this affects Storybook the way it does: causing infinite Suspense loops in RSC components.

@shilman thank you vm for the update 🤞

@boylec That’s amazing. Yeah on SB discord there’s a #contributing channel for help but also feel free to DM or @ me. I often have trouble keeping up!

I don’t know the problem of losing the upstream context of the latter… As for the infinite rendering loop, we confirmed that the situation did not change even when the Storybook version was changed during the investigation of #30033, and that only the Next.js version (React version) was having an effect.

Thanks for looking into it, @boylec!

It seems there are two distinct issues described in this thread:

  • Async RSC in infinite render loop (related to Suspense concurrency changes you have highlighted), tracked in #30033.

  • Some weird behavior wrt “losing” upstream context when a hook is called after the first await (see the next-intl part of my repro, it was like that for a long while now, before becoming an infinite render loop in 8.5.0).

@dmitrc @valentinpalkovic

Looks like the promise now needs to be cached otherwise each render of the rsc/action in storybook returns a new promise object causing the infinite render loop.

Image Image