Skip to content

docs: show how to handle redirects from shared helpers in command functions#16328

Closed
Nic-Polumeyv wants to merge 1 commit into
sveltejs:version-3from
Nic-Polumeyv:docs-command-redirect-helpers
Closed

docs: show how to handle redirects from shared helpers in command functions#16328
Nic-Polumeyv wants to merge 1 commit into
sveltejs:version-3from
Nic-Polumeyv:docs-command-redirect-helpers

Conversation

@Nic-Polumeyv

@Nic-Polumeyv Nic-Polumeyv commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #16275.

The docs promote shared getRequestEvent() helpers, and the canonical requireLogin() example throws redirect(), which explodes with a confusing client error when reused inside a command. This expands the existing Redirects section with the intended pattern, catch with isRedirect and return the location for the client to goto.

For context, allowing redirect() inside command() natively was floated by Conduitry on #14858 and didn't get maintainer buy-in, so this documents the intended pattern rather than a stopgap.

@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from c4ad7d7:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/c4ad7d7c2b24b26840bfe21ad390bd5a0de379c0

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16328

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Jul 11, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: c4ad7d7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Rich-Harris

Copy link
Copy Markdown
Member

You know... I think we've been thinking about this all wrong. My first reaction was that perhaps we should just throw the redirect to the caller...

<!--- file: src/routes/+page.svelte --->
<script>
	import { isRedirect } from '@sveltejs/kit';
	import { goto } from '$app/navigation';
	import { addTodo } from './todos.remote';

	async function add(text) {
		try {
			await addTodo(text);
		} catch (e) {
			if (isRedirect(e)) {
				goto(result.redirect);
			} else {
				throw e;
			}
		}
	}
</script>

...and I do think we should do that, but not because it's a command. The distinction isn't between commands and non-commands, it's between render and non-render.

In render (i.e. in the template, or while otherwise running an effect), a thrown redirect should cause a goto.

Outside render (i.e. in an event handler), it should be like any other exception, and you should have to catch it. The reason for this becomes clear when you consider what currently happens with queries:

const todo = await getTodo(id);
console.log(todo.text);

If getTodo redirects, todo is undefined, directly contradicting both the types and common sense. The second line will throw a TypeError.

This is one of those deeply-obvious-in-retrospect things — it should allow some simplification of the code, and will make commands behave the same as other remote functions with respect to redirects. form might seem like an anomaly here since await submit() in an enhance callback will redirect, but that's because enhanced form submissions are emulating non-enhanced form submissions, which are a form of render.

Working on this locally.

@Nic-Polumeyv

Copy link
Copy Markdown
Contributor Author

I can redo this against your change once it's up, since #16275 will still want a docs example either way, or just close it if docs are part of your change.

@Rich-Harris

Copy link
Copy Markdown
Member

Docs will need to be part of the other PR, so I'll go ahead and close this — thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: clarify that shared getRequestEvent() auth helpers need isRedirect() handling for command()

3 participants