From c4ad7d7c2b24b26840bfe21ad390bd5a0de379c0 Mon Sep 17 00:00:00 2001 From: Nic Polumeyv <162764842+Nic-Polumeyv@users.noreply.github.com> Date: Fri, 10 Jul 2026 21:29:54 -0400 Subject: [PATCH] docs: show how to handle redirects from shared helpers in command functions --- .../20-core-concepts/60-remote-functions.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/documentation/docs/20-core-concepts/60-remote-functions.md b/documentation/docs/20-core-concepts/60-remote-functions.md index 8f0dbdc65dbe..f833c71b7481 100644 --- a/documentation/docs/20-core-concepts/60-remote-functions.md +++ b/documentation/docs/20-core-concepts/60-remote-functions.md @@ -1301,3 +1301,36 @@ Note that some properties of `RequestEvent` are different inside remote function ## Redirects Inside `query`, `form` and `prerender` functions it is possible to use the [`redirect(...)`](@sveltejs-kit#redirect) function. It is *not* possible inside `command` functions, as you should avoid redirecting here. (If you absolutely have to, you can return a `{ redirect: location }` object and deal with it in the client.) + +This matters when a shared helper built on [`getRequestEvent`]($app-server#getRequestEvent) throws a `redirect()` — reused inside a `command`, it will fail with an error. Catch it with [`isRedirect`](@sveltejs-kit#isRedirect) and return the location instead: + +```js +/// file: src/routes/todos.remote.js +import { command } from '$app/server'; +import { isRedirect } from '@sveltejs/kit'; +import { requireLogin } from '$lib/server/auth'; +import * as db from '$lib/server/database'; + +export const addTodo = command('unchecked', async (text) => { + try { + const user = requireLogin(); + await db.addTodo(user, text); + } catch (e) { + if (isRedirect(e)) return { redirect: e.location }; + throw e; + } +}); +``` + +```svelte + + +```