{"record":{"id":"0c886db63902908f","repo":"sveltejs/kit","slug":"redirects-are-not-allowed-in-commands-return-a-re","errorCode":null,"errorMessage":"Redirects are not allowed in commands. Return a result instead and use goto on the client","messagePattern":"Redirects are not allowed in commands\\. Return a result instead and use goto on the client","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/kit/src/runtime/client/remote-functions/command.svelte.js","lineNumber":67,"sourceCode":"\t\t\t\t\t\tif (updates_error) {\n\t\t\t\t\t\t\tthrow updates_error;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst response = await remote_request(\n\t\t\t\t\t\t\t`${base}/${app_dir}/remote/${id}`,\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\t\t\t\t\tpayload: await stringify_command_arg(arg),\n\t\t\t\t\t\t\t\t\trefreshes: Array.from(refreshes ?? [])\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\theaders\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\trefreshes\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (response.redirect) {\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t'Redirects are not allowed in commands. Return a result instead and use goto on the client'\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tfail_unhandled_refreshes(refreshes);\n\n\t\t\t\t\t\treturn response._;\n\t\t\t\t\t} finally {\n\t\t\t\t\t\toverrides?.forEach((fn) => fn());\n\n\t\t\t\t\t\t// Decrement pending count when command completes\n\t\t\t\t\t\tpending_count--;\n\t\t\t\t\t}\n\t\t\t\t})()\n\t\t\t);\n\n\t\tlet updates_called = false;\n\t\tpromise.updates = (...args) => {","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/kit/src/runtime/client/remote-functions/command.svelte.js#L49-L85","documentation":"Remote commands run on the server but execute through the client runtime; returning a redirect response from a command is unsupported because the client would need to perform a full navigation mid-command. Instead, commands must return a result and let the client decide to call goto().","triggerScenarios":"A remote command function whose server handler returns redirect(...) (or sets response.redirect); the client wrapper detects response.redirect after the RPC completes and throws.","commonSituations":"Porting form-action or endpoint code that used `redirect()` into remote commands; post-login redirects implemented server-side; copying +page.server.js patterns into .remote.ts commands.","solutions":["Return a normal result (e.g. { success: true, redirectTo: '/dash' }) from the command","Call goto(result.redirectTo) in the client after awaiting the command","Move flows that genuinely need redirects back to form actions/+page.server.js"],"exampleFix":"// before\n// command server handler\nreturn redirect(303, '/dashboard');\n// after\nreturn { ok: true, redirectTo: '/dashboard' };\n// client\nconst res = await createAccount(...);\nif (res.redirectTo) goto(res.redirectTo);","handlingStrategy":"try-catch","validationCode":"// server side: never call redirect() inside a command handler\nfunction assertNoRedirect(result) {\n  if (result && result.redirect) throw new Error('commands cannot redirect; return data instead');\n  return result;\n}","typeGuard":null,"tryCatchPattern":"try { const res = await myCommand(...); } catch (e) { if (e.message.includes('Redirects are not allowed in commands')) { /* restructure to result + client goto */ } else { throw e; } }","preventionTips":["Design commands to return plain serializable results","Handle post-command routing exclusively on the client with goto","Reserve redirect() for form actions and load functions"],"tags":["remote-functions","redirect","api-design"],"backgroundTag":"redirect-in-command","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}