{"record":{"id":"298324e4343e21d0","repo":"remix-run/react-router","slug":"getrequest-must-be-called-from-within-a-react-serv","errorCode":null,"errorMessage":"getRequest must be called from within a React Server render context","messagePattern":"getRequest must be called from within a React Server render context","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/react-router/lib/rsc/server.rsc.ts","lineNumber":98,"sourceCode":"\ntype ServerContext = {\n  redirect?: Response;\n  request: Request;\n  runningAction: boolean;\n};\n\nconst globalVar = (typeof globalThis !== \"undefined\" ? globalThis : global) as {\n  ___reactRouterServerStorage___?: AsyncLocalStorage<ServerContext>;\n};\n\nconst ServerStorage = (globalVar.___reactRouterServerStorage___ ??=\n  new AsyncLocalStorage<ServerContext>());\n\nexport function getRequest() {\n  const ctx = ServerStorage.getStore();\n\n  if (!ctx)\n    throw new Error(\n      \"getRequest must be called from within a React Server render context\",\n    );\n  return ctx.request;\n}\n\nexport const redirect: typeof baseRedirect = (...args) => {\n  const response = baseRedirect(...args);\n\n  const ctx = ServerStorage.getStore();\n  if (ctx && ctx.runningAction) {\n    ctx.redirect = response;\n  }\n\n  return response;\n};\n\nexport const redirectDocument: typeof baseRedirectDocument = (...args) => {\n  const response = baseRedirectDocument(...args);","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/remix-run/react-router/blob/1fd704a7dabcbe3ae09d7387b460e6acaba30ec1/packages/react-router/lib/rsc/server.rsc.ts#L80-L116","documentation":"`getRequest()` reads the current `Request` from an `AsyncLocalStorage`-backed server context. If `ServerStorage.getStore()` returns `undefined`, the call is outside any `run(...)` scope established by React Router's RSC runtime, so there is no current request and the function refuses to continue.","triggerScenarios":"Calling `getRequest()` (or RSC-aware `redirect`/`replace` that internally relies on it) at module top-level, in a utility called outside the React Server render pass, or in a worker/thread that doesn't inherit the AsyncLocalStorage context.","commonSituations":"Calling `getRequest()` from a standalone script, a route module's module scope (not inside loader/action/server component), or a non-RSC handler; using the RSC server API in a Data-mode or Declarative-mode app where no server render context is ever established.","solutions":["Only call `getRequest()` from inside a server component, loader, action, or middleware running within the RSC render pass.","If you need the request in a helper, accept it as a parameter from the loader/action rather than reading it implicitly.","Confirm the RSC runtime is actually initialized (RSC Framework or RSC Data mode) — in non-RSC modes there is no server context."],"exampleFix":"// before (module scope)\nconst req = getRequest(); // throws\n\n// after (inside server component)\nexport default async function Route() {\n  const req = getRequest();\n  return <pre>{req.url}</pre>;\n}","handlingStrategy":"try-catch","validationCode":"// Only call inside the render pass; gate with an explicit flag\nasync function safeGetRequest() {\n  try {\n    return getRequest();\n  } catch {\n    return undefined;\n  }\n}","typeGuard":"function isInServerContext(): boolean {\n  return Boolean((globalThis as any).___reactRouterServerStorage___?.getStore?.());\n}","tryCatchPattern":"let request: Request | undefined;\ntry {\n  request = getRequest();\n} catch {\n  request = undefined; // not in an RSC render scope\n}","preventionTips":["Call `getRequest()` only inside server components/loaders/actions.","Pass the Request explicitly to helpers instead of relying on implicit context.","Confirm RSC mode is enabled before relying on server context."],"tags":["rsc","server-context","async-local-storage","getrequest"],"backgroundTag":null,"analyzedSha":"1fd704a7dabcbe3ae09d7387b460e6acaba30ec1","analyzedAt":"2026-08-12T13:54:57.804Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}