Repository navigation
docs: show how to handle redirects from shared helpers in command functions - #16328
Nic-Polumeyv wants to merge 1 commit into
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/c4ad7d7c2b24b26840bfe21ad390bd5a0de379c0Open in Note This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed. |
|
|
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 In render (i.e. in the template, or while otherwise running an effect), a thrown redirect should cause a 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 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. Working on this locally. |
|
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. |
|
Docs will need to be part of the other PR, so I'll go ahead and close this — thanks |
|
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. |
Fixes #16275.
The docs promote shared
getRequestEvent()helpers, and the canonicalrequireLogin()example throwsredirect(), which explodes with a confusing client error when reused inside acommand. This expands the existing Redirects section with the intended pattern, catch withisRedirectand return the location for the client togoto.For context, allowing
redirect()insidecommand()natively was floated by Conduitry on #14858 and didn't get maintainer buy-in, so this documents the intended pattern rather than a stopgap.