Repository: remix-run/react-router
File: packages/react-router/lib/rsc/server.ssr.tsx (TypeScript)

Code before:
import * as React from "react";
import { RSCRouterContext, type DataRouteObject } from "../context";
import { FrameworkContext } from "../dom/ssr/components";
import type { FrameworkContextObject } from "../dom/ssr/entry";
import { createStaticRouter, StaticRouterProvider } from "../dom/server";
import { injectRSCPayload } from "./html-stream/server";
import { RSCRouterGlobalErrorBoundary } from "./errorBoundaries";
import { shouldHydrateRouteLoader } from "../dom/ssr/routes";
import type { RSCPayload } from "./server.rsc";

const react18compatibleUse = (() => {
  type Usable<T> = Promise<T> | React.Context<T>;

  const errorUse = function <T>(_: Usable<T>): T {
    throw new Error("React Router v7 requires React 19+ for RSC features.");
  };

  const functionName = "use";
  const realUse = (React as any)[functionName];

  return function <T>(promise: Usable<T>): T {
    return (realUse || errorUse)(promise);
  };
})();

export type SSRCreateFromReadableStreamFunction = (
  body: ReadableStream<Uint8Array>,
) => Promise<unknown>;

/**
 * Routes the incoming [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
 * to the [RSC](https://react.dev/reference/rsc/server-components) server and
 * appropriately proxies the server response for data / resource requests, or
 * renders to HTML for a document request.
 *
 * @example
 * import { createFromReadableStream } from "@vitejs/plugin-rsc/ssr";
 * import * as ReactDomServer from "react-dom/server.edge";
 * import {
 *   unstable_RSCStaticRouter as RSCStaticRouter,
 *   unstable_routeRSCServerRequest as routeRSCServerRequest,
 * } from "react-router";
 *
 * routeRSCServerRequest({
 *   request,
 *   fetchServer,
 *   createFromReadableStream,
 *   async renderHTML(getPayload) {
 *     const payload = await getPayload();

Review comment (suggestion):
Minor adjustment to use the pattern we've followed for this type of stuff before: https://github.com/remix-run/react-router/blob/v6/packages/react-router-dom/index.tsx#L410

```suggestion
// Safe version of React.use() that will not cause compilation errors against
// React 18 and will result in a runtime error if used (you can't use RSC against
// React 18).
const REACT_USE = "use";
const useImpl = (React as any)[REACT_USE];

function useSafe<T>(promise: Promise<T> | React.Context<T>): T {
  if (useImpl) {
    return useImpl(promise);
  }
  throw new Error("React Router v7 requires React 19+ for RSC features.");
}
```

Code after:
import * as React from "react";
import { RSCRouterContext, type DataRouteObject } from "../context";
import { FrameworkContext } from "../dom/ssr/components";
import type { FrameworkContextObject } from "../dom/ssr/entry";
import { createStaticRouter, StaticRouterProvider } from "../dom/server";
import { injectRSCPayload } from "./html-stream/server";
import { RSCRouterGlobalErrorBoundary } from "./errorBoundaries";
import { shouldHydrateRouteLoader } from "../dom/ssr/routes";
import type { RSCPayload } from "./server.rsc";

// Safe version of React.use() that will not cause compilation errors against
// React 18 and will result in a runtime error if used (you can't use RSC against
// React 18).
const REACT_USE = "use";
const useImpl = (React as any)[REACT_USE];

function useSafe<T>(promise: Promise<T> | React.Context<T>): T {
  if (useImpl) {
    return useImpl(promise);
  }
  throw new Error("React Router v7 requires React 19+ for RSC features.");
}

export type SSRCreateFromReadableStreamFunction = (
  body: ReadableStream<Uint8Array>,
) => Promise<unknown>;

/**
 * Routes the incoming [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
 * to the [RSC](https://react.dev/reference/rsc/server-components) server and
 * appropriately proxies the server response for data / resource requests, or
 * renders to HTML for a document request.
 *
 * @example
 * import { createFromReadableStream } from "@vitejs/plugin-rsc/ssr";
 * import * as ReactDomServer from "react-dom/server.edge";
 * import {
 *   unstable_RSCStaticRouter as RSCStaticRouter,
 *   unstable_routeRSCServerRequest as routeRSCServerRequest,
 * } from "react-router";
 *
 * routeRSCServerRequest({
 *   request,
 *   fetchServer,
 *   createFromReadableStream,
 *   async renderHTML(getPayload) {
 *     const payload = await getPayload();
 *
 *     return await renderHTMLToReadableStream(