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

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
Member

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
Member 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

@bertybot

Copy link
Copy Markdown
Contributor

Hey just throwing my opinion on this since I ran into this problem as well. Redirects in remote functions is a weird adjustment if you are primarily used to using Sveltekit page loaders. How redirects work in remote functions seemed buggy at first to me until I understood them a bit more.

It primarily effects a common use case I have. All of my apps are internal admin apps where every page is authenticated. If you are not authenticated you are sent to a login screen this is handled in a hook. I have code so that the users current url is saved with the request so that when you come back you can continue from where you were.

In page loaders this was handled pretty easily since every request was for the page it self. The URL information was for the page and the redirect was easy, even if it was a data request parsing the current page was easy. With remote functions the request is now to the remote and you have little to no information on what page the user is actually on, and this function breaks down. This behavior makes sense because remotes are supposed to be more general, but now this leaves me in a pickle.

Trying to parse URL information in the remote from the request fails with a warning (this makes sense but, can be frustrating when you run across this problem). Basically the only solution I could find to replicate this original functionality is to make a wrapper function for every command type that requires the page store every time so that you can replicate this. To me this is a lot more complicated then what I had originally with loaders.

I will also just add that if a redirect is thrown from a hook the remote is no longer type safe and will return the redirect type, usually causing an undefined error on the frontend that is hard to debug. Maybe some better DX there would help?

I did a lot of this just because I heard rumors that remotes are supposed to completely replace loaders. If this is not the case maybe I was mistaken. Otherwise, maybe some helpful docs on how remotes can replace a loader function would be helpful. Right now how to send page state like you do in loaders isn't really clear. I am using the page store but is that correct? Is there anyway that getRequestEvent could also contain information of what page the user is on even in remotes? Idk just thought I would put my current thoughts here.

All in all I think remotes are a great. So, far this is the only real annoyance I have run into with them.

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()

4 participants