Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/plenty-olives-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

add RSC unstable_getRequest
32 changes: 31 additions & 1 deletion integration/rsc/rsc-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,12 @@ implementations.forEach((implementation) => {
id: "render-route-error-response",
path: "render-route-error-response/:id?",
lazy: () => import("./routes/render-route-error-response/home"),
}
},
{
id: "get-request",
path: "/get-request",
lazy: () => import("./routes/get-request/get-request"),
},
],
},
] satisfies RSCRouteConfig;
Expand Down Expand Up @@ -1549,6 +1554,17 @@ implementations.forEach((implementation) => {
return <p>Oh no D:</p>;
}
`,

"src/routes/get-request/get-request.tsx": js`
import { unstable_getRequest as getRequest } from "react-router";

export function action() { return null; }

export default function GetRequest() {
const request = getRequest();
return <p>{request.method}</p>;
}
`,
},
});
});
Expand Down Expand Up @@ -1892,6 +1908,20 @@ implementations.forEach((implementation) => {
);
await expect(page.getByText("400 Oh no! Test")).toBeAttached();
});

test("Supports getRequest in server components", async ({ page }) => {
await page.goto(`http://localhost:${port}/get-request`);
await expect(page.getByText("GET")).toBeAttached();

const response = await page.request.fetch(
`http://localhost:${port}/get-request`,
{
method: "POST",
},
);
const body = await response.text();
expect(body).toContain("<p>POST</p>");
});
});

test.describe("Server Actions", () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/react-router/index-react-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// RSC APIs
export { matchRSCServerRequest as unstable_matchRSCServerRequest } from "./lib/rsc/server.rsc";
export {
getRequest as unstable_getRequest,
matchRSCServerRequest as unstable_matchRSCServerRequest,
} from "./lib/rsc/server.rsc";

export type {
DecodeActionFunction as unstable_DecodeActionFunction,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ export {
export { RSCDefaultRootErrorBoundary as UNSAFE_RSCDefaultRootErrorBoundary } from "./lib/rsc/errorBoundaries";

// Re-export of RSC types
import type { matchRSCServerRequest } from "./lib/rsc/server.rsc";
import type { getRequest, matchRSCServerRequest } from "./lib/rsc/server.rsc";
export declare const unstable_getRequest: typeof getRequest;
export declare const unstable_matchRSCServerRequest: typeof matchRSCServerRequest;

export type {
Expand Down
12 changes: 12 additions & 0 deletions packages/react-router/lib/rsc/server.rsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const WithHydrateFallbackProps: typeof WithHydrateFallbackPropsType =

type ServerContext = {
redirect?: Response;
request: Request;
runningAction: boolean;
};

Expand All @@ -88,6 +89,16 @@ const globalVar = (typeof globalThis !== "undefined" ? globalThis : global) as {
const ServerStorage = (globalVar.___reactRouterServerStorage___ ??=
new AsyncLocalStorage<ServerContext>());

export function getRequest() {
const ctx = ServerStorage.getStore();

if (!ctx)
throw new Error(
"getRequest must be called from within a React Server render context",
);
return ctx.request;
}

export const redirect: typeof baseRedirect = (...args) => {
const response = baseRedirect(...args);

Expand Down Expand Up @@ -782,6 +793,7 @@ async function generateRenderResponse(

let actionResult: Promise<unknown> | undefined;
const ctx: ServerContext = {
request,
runningAction: false,
};

Expand Down