{"record":{"id":"8fa3563f438f11e7","repo":"remix-run/react-router","slug":"you-cannot-call-runclientmiddleware-from-a-sta","errorCode":null,"errorMessage":"You cannot call `runClientMiddleware()` from a static handler `dataStrategy`. Middleware is run outside of `dataStrategy` during SSR in order to bubble up the Response.  You can enable middleware via the `respond` API in `query`/`queryRoute`","messagePattern":"You cannot call `runClientMiddleware\\(\\)` from a static handler `dataStrategy`\\. Middleware is run outside of `dataStrategy` during SSR in order to bubble up the Response\\.  You can enable middleware via the `respond` API in `query`/`queryRoute`","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/react-router/lib/router/router.ts","lineNumber":6573,"sourceCode":"  }\n\n  // Send all matches here to allow for a middleware-type implementation.\n  // handler will be a no-op for unneeded routes and we filter those results\n  // back out below.\n  let dataStrategyArgs: Omit<\n    DataStrategyFunctionArgs<unknown>,\n    \"fetcherKey\" | \"runClientMiddleware\"\n  > = {\n    request,\n    url: createDataFunctionUrl(request, path),\n    pattern: getRoutePattern(matches),\n    params: matches[0].params,\n    context: scopedContext,\n    matches,\n  };\n  let runClientMiddleware = isStaticHandler\n    ? () => {\n        throw new Error(\n          \"You cannot call `runClientMiddleware()` from a static handler \" +\n            \"`dataStrategy`. Middleware is run outside of `dataStrategy` during \" +\n            \"SSR in order to bubble up the Response.  You can enable middleware \" +\n            \"via the `respond` API in `query`/`queryRoute`\",\n        );\n      }\n    : (cb: DataStrategyFunction<RouterContextProvider>) => {\n        let typedDataStrategyArgs =\n          dataStrategyArgs as DataStrategyFunctionArgs<RouterContextProvider>;\n        return runClientMiddlewarePipeline(typedDataStrategyArgs, () => {\n          return cb({\n            ...typedDataStrategyArgs,\n            fetcherKey,\n            runClientMiddleware: () => {\n              throw new Error(\n                \"Cannot call `runClientMiddleware()` from within an \" +\n                  \"`runClientMiddleware` handler\",\n              );","sourceCodeStart":6555,"sourceCodeEnd":6591,"githubUrl":"https://github.com/remix-run/react-router/blob/1fd704a7dabcbe3ae09d7387b460e6acaba30ec1/packages/react-router/lib/router/router.ts#L6555-L6591","documentation":"When `createStaticHandler` runs a custom `dataStrategy` during SSR (`isStaticHandler === true`), the `runClientMiddleware` field is a function that always throws. Middleware cannot run from within `dataStrategy` during SSR because the framework must let the Response bubble up through the static handler's `respond` API instead.","triggerScenarios":"User-supplied `dataStrategy` calls `args.runClientMiddleware(...)` while the handler is the static (SSR) one created by `createStaticHandler({ dataStrategy })`.","commonSituations":"Sharing one `dataStrategy` between client and server without branching on context; copy-pasting a client-side middleware-driving dataStrategy into `createStaticHandler`.","solutions":["Do not call `runClientMiddleware()` from a `dataStrategy` used with `createStaticHandler`.","Branch the dataStrategy: run middleware on the client only, or drive SSR middleware via the `respond` API in `query`/`queryRoute`.","Factor the shared logic out so the SSR variant omits the `runClientMiddleware` call."],"exampleFix":"// before\ncreateStaticHandler(routes, {\n  dataStrategy: (args) => args.runClientMiddleware(() => defaultStrategy(args)),\n});\n\n// after\ncreateStaticHandler(routes, {\n  dataStrategy: defaultStrategy, // no runClientMiddleware on SSR\n});","handlingStrategy":"validation","validationCode":"function isStaticDataStrategyArgs(args: any): boolean {\n  return typeof args.runClientMiddleware === 'function' && /static/.test(args.runClientMiddleware.toString());\n}\n// branch the dataStrategy:\nconst dataStrategy = (args) =>\n  isStaticDataStrategyArgs(args)\n    ? defaultStrategy(args)\n    : args.runClientMiddleware(() => defaultStrategy(args));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep one dataStrategy per handler type (static vs client).","Don't share client-only middleware-driving code with the SSR handler.","Drive SSR middleware via the `respond` API."],"tags":["ssr","middleware","data-strategy","static-handler"],"backgroundTag":null,"analyzedSha":"1fd704a7dabcbe3ae09d7387b460e6acaba30ec1","analyzedAt":"2026-08-12T13:54:57.804Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}