storybook: [Bug]: RSC stories that use "react-server" exports do not get bundled correctly

Describe the bug

Hi there! šŸ‘‹

Apollo has a package for integrating streaming + apollo client found here: https://github.com/apollographql/apollo-client-nextjs/

Recently they migrated to react-server conditional exports (RFC found here: https://github.com/reactjs/rfcs/blob/main/text/0227-server-module-conventions.md#react-server-conditional-exports) such that code that’s supposed to only run on the server never gets imported in a client bundle. This obviously breaks in storybook where the RSC implementation is actually running in the browser. Does anyone have any suggestions around how to make this work? I expect more and more packages are going to use react-server for exports/bundling so it feels like this is something storybook should be prepared to handle.

I provided a simple repo below that could be a starting point for coming up with a fix - it’s just a barebones Next app with storybook 8 installed.

Reproduction link

https://github.com/shawngustaw/storybook-rsc-repro

Reproduction steps

  1. npm i
  2. npm run dev
  3. verify Pokemon component renders correctly in NextJS app router using RSC’s
  4. npm run storybook
  5. verify the same does not run within storybook

System

Storybook Environment Info:

  System:
    OS: macOS 14.4.1
    CPU: (12) arm64 Apple M2 Pro
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.17.0 - ~/.nvm/versions/node/v18.17.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v18.17.0/bin/yarn
    npm: 9.6.7 - ~/.nvm/versions/node/v18.17.0/bin/npm <----- active
    pnpm: 9.1.4 - ~/.nvm/versions/node/v18.17.0/bin/pnpm
  Browsers:
    Chrome: 125.0.6422.61
    Safari: 17.4.1
  npmPackages:
    @storybook/addon-essentials: ^8.1.5 => 8.1.5 
    @storybook/addon-interactions: ^8.1.5 => 8.1.5 
    @storybook/addon-links: ^8.1.5 => 8.1.5 
    @storybook/addon-onboarding: ^8.1.5 => 8.1.5 
    @storybook/blocks: ^8.1.5 => 8.1.5 
    @storybook/nextjs: ^8.1.5 => 8.1.5 
    @storybook/react: ^8.1.5 => 8.1.5 
    @storybook/test: ^8.1.5 => 8.1.5 
    eslint-plugin-storybook: ^0.8.0 => 0.8.0 
    storybook: ^8.1.5 => 8.1.5

Additional context

I brought this up with the Apollo team here: https://github.com/apollographql/apollo-client-nextjs/issues/307 No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (9 by maintainers)

Most upvoted comments

I have no time at the moment to write an adequate reply.

But I do appreciate your feedback, @phryneas. The point that even react itself is using this convention for loading a different cache with react-server conditions has made us realize that we need to address this ā€œsomehowā€ from the storybook side.

We are still discussing how we can address this, but we are too busy releasing 8.3 at the moment. So I will write a more proper longer reaction later!

We are finally working on a new architecture for RSC. See: https://github.com/storybookjs/storybook/issues/30317#issuecomment-2806544121

We will use 2 different bundles (a RSC and client bundle), but run both in the browser. 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

I’m not sure if that is really more simple. If you import @apollo/experimental-nextjs-app-support, TS thinks that all exports are available, and TS won’t complain if you use them, even though in fact they will be only conditionally available.

The same is true for the react package itself, where some exports like useState are available in Client Components, but crash the bundler for Server Components (so during build time, long before run time - just like they would with the Apollo Client RSC package). We are really following React’s conventions here that they have intended to be used in the ecosystem.

As for the ā€œmore simpleā€ part: a lot of the exports are available in all variants, but with different implementations per condition.

Previously, we had users import from /rsc in RSC and from /ssr in SSR. It turned out to be extremely confusing for them, and lead to many copy-paste errors that I’ve spotted in issues over the time. Each of those ā€œwrong importsā€ lead to bugs or the library flat out not working as expected. It is really a lot less prone to errors if they are available under the same export and just swap for the right implementation transparently.

We never run RSC in the server.

You can RSC in the browser as well, using the RSC build of React, and then send the result to the SSR build of React, and then hydrate it with the Browser build of React - all three steps in the same browser. (Preferrably in separated workers) They are not bound to a server, despite the name. They already run in all kind of edge runtimes, which are often much closer to a browser than node.


I’m really sorry, I know you deserve a longer answer, but it’s very late here and I’m very tired.

Generally, it boils down to this: This is a pattern that the React team intends to be used throughout the ecosystem and that they are modelling themselves.

I don’t want Apollo Client to become the place this is fought out on. We had enough discussions around bundling with the React and Next.js team over the last year. If you have complaints about this pattern, please take them up to the React team. We are just one library caught in the middle that is a bit faster in implementation than many others, but many others will follow once React 19 officially releases and you’ll have this fight over and over again, until you either convince the React team that this is not a good pattern (to be honest, at this point it’s probably years too late for having that discussion, see the RFC linked in the first comment) or actually run RSCs with the RSC build in the browser - which the React team can probably help you with.


PS: Rereading this a bit more:

I see where you are coming from with the idea of ā€œonly one runtimeā€ - but running RSC code and Browser code in the same environment/scope seems very dangerous to me. What will window be? undefined or globalThis? You’d run the risk of leaking data that would usually be separated by processes through a now-shared global scope. Your storybook stories would be far removed from ā€œrealā€ behaviour, having bugs that can only occur in that environment, and hiding bugs that would occur in a real-life situation. Yes, you will lose the simplicity of unit testing only a single environment. But that’s because RSC are not simple. Anyone who starts using RSC will need to understand that they write code for three different environments, and as a result they will also need to wrap their head around writing tests for these three environments. This is something where you can innovate testing patterns - but please keep the real constraints in place. Nobody will be happy if they have amazing Storybook stories that can never work that way in a real application.

The idea of the ā€œcombinedā€ export would really be an export just for storybook and we’d have to write a completely separate implementation that ignored normal React behavior to work under the constraints you create in StoryBook that are not shared by any real-life deployment of a React application. You can’t expect every React library to do that šŸ˜•


PPS: one more example where this is dangerous:

React.cache is available in both environments, but behaves completely different - it just doesn’t do anything in the browser, which will completely confuse a library that is written against the assumption to run in RSC, where it memoizes method calls. We use that in our RSC code - and we rely on the memoization. Userland code using registerApolloClient with the wrong React.cache in the background will create new Apollo Client instances all over again, instead of just creating one for the whole request. It will start over with an empty cache every time, and in the best case just make a ton of extra network requests and in the worst case completely fail because of missing cache contents.