From 02d7aed70adbd6062cef48f73eb24551d2a3e987 Mon Sep 17 00:00:00 2001 From: Hashim Khan Date: Thu, 23 Jul 2026 19:40:29 +0500 Subject: [PATCH] docs: show isRedirect handling for shared helpers in commands Clarify that redirect()-based auth helpers must be caught with isRedirect inside command() and returned as a client-handled result. --- .../20-core-concepts/60-remote-functions.md | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/documentation/docs/20-core-concepts/60-remote-functions.md b/documentation/docs/20-core-concepts/60-remote-functions.md index dc97b757bb29..0b9d13d9e49e 100644 --- a/documentation/docs/20-core-concepts/60-remote-functions.md +++ b/documentation/docs/20-core-concepts/60-remote-functions.md @@ -1308,3 +1308,60 @@ 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 you share helpers that call `redirect()`, for example an auth helper built with [`getRequestEvent`]($app-server#getRequestEvent): + +```ts +/// file: auth.remote.js +import { redirect } from '@sveltejs/kit'; +import { getRequestEvent } from '$app/server'; + +export function requireLogin() { + const { locals, url } = getRequestEvent(); + + if (!locals.user) { + const location = new URL('/login', url.origin); + location.searchParams.set('redirectTo', url.pathname); + redirect(307, location); + } + + return locals.user; +} +``` + +That helper works inside `query` and `form`, but calling it from a `command` will fail because redirects are rejected there. Catch the redirect on the server with [`isRedirect`](@sveltejs-kit#isRedirect) and return a result the client can handle with [`goto`]($app-navigation#goto): + +```ts +/// file: save.remote.js +import { isRedirect } from '@sveltejs/kit'; +import { command } from '$app/server'; +import { requireLogin } from './auth.remote'; + +export const save = command(async (data) => { + try { + requireLogin(); + } catch (e) { + if (isRedirect(e)) { + return { error: 'UNAUTHENTICATED', location: e.location }; + } + throw e; + } + + // ... +}); +``` + +```ts +/// file: +page.svelte + +```