Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid duplicate RSC Data error logs during SSR
  • Loading branch information
brophdawg11 committed Nov 20, 2026
commit 7e901039baaf7cd94f453756587c06828e285edc
4 changes: 0 additions & 4 deletions packages/react-router/.changes/patch.rsc-log-server-errors.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Report loader, action, and middleware errors through `unstable_matchRSCServerRequest`'s `onError` and log request errors with `console.error` by default when no `onError` is provided

- Report middleware errors that occur after a form action consumes the request body.
- Avoid logging route errors again when rendering the default error boundary during SSR in RSC Data and RSC Framework Mode.
42 changes: 41 additions & 1 deletion packages/react-router/__tests__/rsc/server-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from "react";
import { renderToReadableStream } from "react-dom/server.edge";
import { renderToString } from "react-dom/server.node";

import {
Expand All @@ -7,7 +8,10 @@ import {
type RSCMatch,
type RSCRouteConfigEntry,
} from "../../lib/rsc/server.rsc";
import { routeRSCServerRequest } from "../../lib/rsc/server.ssr";
import {
RSCStaticRouter,
routeRSCServerRequest,
} from "../../lib/rsc/server.ssr";
import { URL_LIMIT } from "../../lib/dom/ssr/fog-of-war";

describe("RSC server", () => {
Expand Down Expand Up @@ -804,6 +808,42 @@ describe("RSC server", () => {
expect(consoleError).toHaveBeenCalledWith(new Error("LOADER_ERROR"));
});

test("logs an RSC Data loader error once when SSR renders the default boundary", async () => {
let error = new Error("LOADER_ERROR");
let payload: RSCMatch["payload"] | undefined;
let response = await matchRSCServerRequest({
createTemporaryReferenceSet: () => ({}),
request: new Request("https://remix.run/"),
routes: [
{
id: "root",
path: "/",
Component: () => null,
loader() {
throw error;
},
},
],
generateResponse(match) {
payload = match.payload;
return new Response(null, { status: match.statusCode });
},
});
expect(response.status).toBe(500);

let decoded = Object.assign(Promise.resolve(payload!), {
formState: Promise.resolve(undefined),
});
let stream = await renderToReadableStream(
React.createElement(RSCStaticRouter, { getPayload: () => decoded }),
);
expect(await new Response(stream).text()).toContain(
"Unexpected Application Error!",
);
expect(consoleError).toHaveBeenCalledTimes(1);
expect(consoleError).toHaveBeenCalledWith(error);
});

test("logs the underlying error for internal error responses", async () => {
await match(new Request("https://remix.run/does-not-exist"));
expect(consoleError).toHaveBeenCalledTimes(1);
Expand Down
1 change: 1 addition & 0 deletions packages/react-router/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const DataRouterNavigationContext =
DataRouterNavigationContext.displayName = "DataRouterNavigation";

export const RSCRouterContext = React.createContext<boolean>(false);
export const RSCErrorAlreadyReportedContext = React.createContext(false);

export function useIsRSCRouterContext(): boolean {
return React.useContext(RSCRouterContext);
Expand Down
12 changes: 8 additions & 4 deletions packages/react-router/lib/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
IsDataRouteContext,
LocationContext,
NavigationContext,
RSCErrorAlreadyReportedContext,
RSCRouterContext,
RouteContext,
RouteErrorContext,
Expand Down Expand Up @@ -975,6 +976,7 @@ export function useRoutesImpl(

function DefaultErrorComponent() {
let error = useRouteError();
let alreadyReported = React.useContext(RSCErrorAlreadyReportedContext);
let message = isRouteErrorResponse(error)
? `${error.status} ${error.statusText}`
: error instanceof Error
Expand All @@ -987,10 +989,12 @@ function DefaultErrorComponent() {

let devInfo = null;
if (ENABLE_DEV_WARNINGS) {
console.error(
"Error handled by React Router default ErrorBoundary:",
error,
);
if (!alreadyReported) {
console.error(
"Error handled by React Router default ErrorBoundary:",
error,
);
}

devInfo = (
<>
Expand Down
3 changes: 1 addition & 2 deletions packages/react-router/lib/rsc/errorBoundaries.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from "react";
import { RSCErrorAlreadyReportedContext } from "../context";
import { useRouteError } from "../hooks";
import type { Location } from "../router/history";
import { ENABLE_DEV_WARNINGS, isRouteErrorResponse } from "../router/utils";

export const RSCErrorAlreadyReportedContext = React.createContext(false);

type RSCRouterGlobalErrorBoundaryProps = React.PropsWithChildren<{
location: Location;
}>;
Expand Down
7 changes: 2 additions & 5 deletions packages/react-router/lib/rsc/server.ssr.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as React from "react";
import { RSCRouterContext } from "../context";
import { RSCErrorAlreadyReportedContext, RSCRouterContext } from "../context";
import { FrameworkContext } from "../dom/ssr/components";
import type { FrameworkContextObject } from "../dom/ssr/entry";
import { SINGLE_FETCH_REDIRECT_STATUS } from "../dom/ssr/single-fetch";
import { createStaticRouter, StaticRouterProvider } from "../dom/server";
import { injectRSCPayload } from "./html-stream/server";
import {
RSCErrorAlreadyReportedContext,
RSCRouterGlobalErrorBoundary,
} from "./errorBoundaries";
import { RSCRouterGlobalErrorBoundary } from "./errorBoundaries";
import { shouldHydrateRouteLoader } from "../dom/ssr/routes";
import type { RSCPayload } from "./server.rsc";
import { createRSCRouteModules } from "./route-modules";
Expand Down