KEMBAR78
Bug: `use()` yields incorrect value during first SSR · Issue #27731 · facebook/react · GitHub
Skip to content

Bug: use() yields incorrect value during first SSR #27731

@ziir

Description

@ziir

During the first server-side streamed rendering attempt of a RSC tree including a component that is wrapped in <Suspense> & leverages use(), the owner component does not suspend, and its use() yields an unexpected value.

Subsequent requests / SSR attempts result in the wrapped component suspending as expected, and use() yielding the expected value.

// server-root.jsx
function ServerRoot({ flightResponse }) {
  return (
    <html>
      <body>
        {/* use(flightResponse) exhibits same issue */}
        {flightResponse}
      </body>
    </html>
  );
}
// react-html.js
const flightResponse = createFromNodeStream(...);

const renderStream = renderToPipeableStream(
  <ServerRoot flightResponse={flightResponse} />,
  options
  }
);
// app.js server component
import { Suspense } from "react";

import { ClientConsumer } from "./client-consumer";

export function App() {
  const promise = new Promise((resolve) =>
    setTimeout(() => resolve(["foo", "bar"]), 1_000)
  );

  return (
    <Suspense fallback="loading ...">
      <ClientConsumer promise={promise}>hello world</ClientConsumer>
    </Suspense>
  );
}
// client-consumer.js client component
"use client";
import { use } from "react";

export function ClientConsumer({ promise, children }) {
  console.log("ClientConsumer before the use()");
  const value = use(promise);
  console.log("ClientConsumer after the use()", value);

  return children;
}

React version:

  • react: "18.3.0-canary-bbb9cb116-20231117",
  • react-dom: "18.3.0-canary-bbb9cb116-20231117",
  • react-server-dom-webpack: "18.3.0-canary-bbb9cb116-20231117",

Steps To Reproduce

Link to code example: https://github.com/ziir/rsc-bug-repro

  1. Clone, set-up & start minimal reproduction
  2. Navigate to http://localhost:3000

The current behavior

On the first SSR attempt only:

  • The ClientConsumer component owning the use(promise) hook does not suspend while the promise is pending.
  • use(promise) yields an unexpected value: a JSX element matching the App parent server component.

Screenshot from 2023-11-21 00-24-26

The expected behavior

As in the subsequent SSR requests:

  • The ClientConsumer component owning the use(promise) hook suspends while the promise is pending.
  • use(promise) yields the expected fulfilled promise value: ['foo', 'bar']

Screenshot from 2023-11-21 00-25-42

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: StaleAutomatically closed due to inactivityStatus: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions