-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug?]: Server functions that throw or return redirects when called in an action do not redirect #1512
Comments
I think I'm encountering this bug in a different form. Following the Protected Routes example and I cannot get the redirect to work even when my definition is:
|
I was able to reproduce the issue using the following route import { type RouteDefinition } from '@solidjs/router'
import { cache, createAsync, redirect } from '@solidjs/router'
import { getRequestEvent } from 'solid-js/web'
const exampleAction = cache(async () => {
'use server'
const location = new URL(getRequestEvent()!.request.url)
return redirect(`${location.origin}/target`)
}, 'exampleAction')
export const route = {
load() {
// redirect will start working if uncommented:
// void exampleAction()
},
} satisfies RouteDefinition
export default function Component() {
const data = createAsync(() => exampleAction())
return <h1>Result: {data()}</h1>
} With the route above, when you navigate to Unless you trigger an action with redirect inside |
@mmmoli Redirects are ignored inside @frenzzy This seems like a separate issue, the redirect should happen on the client whether |
@apatrida looks like the |
@Brendonovich thanks for the follow-up. I was, yes. Question: will your PR fix error handling for load functions too, or is that by design? I'll keep an eye on the docs to see what the behaviour is supposed to be. |
@mmmoli That redirect should work, is your setup something like this? https://stackblitz.com/edit/github-fk8exj-zbxuey?file=src%2Froutes%2Findex.tsx,src%2Froutes%2Fredirect.tsx,src%2Fapp.tsx
wym by error handling? i don't think load functions are meant to handle errors at all |
Yes? Forked with more specifics: https://stackblitz.com/edit/github-fk8exj-waqwjq?file=src%2Fcomponents%2Fuser.tsx I'm brand-new to solidjs so might have a different mental model. Features:
|
@mmmoli The stackblitz you provided seems to be working as expected, and the behaviour isn't affected by the presence of
If you do think there's a bug there I'd suggest opening a new issue with a dedicated reproduction, I'm not sure it's related to this bug. |
Ok yeah this was an oversight that I probably introduced towards the end of the beta phase. We definitely should be throwing Responses that were thrown. It is interesting if we should always be throwing them. The types suggest we should but maybe the types are wrong. I see this going one of 2 ways.
In any case since this takes work my gut is to just merge the current PR for now so that the immediate issue is resolved. And then probably make a choice and do a minor release of the router and/or Start. But would appreciate thoughts/feedback on the direction. I think option 1 is better long term if I can figure out because it makes this into more of a standard protocol rather than a special cased hack. But sometimes hacks work really nicely. |
imo option 1 is better, seems more predictable and typesafe. 'You return a Response, you get a Response' sounds more inline with the isomorphic nature of server functions.
This would definitely be doable, I made a small symbol-based implementation the other day but it could just as easily be done with a custom Response class. |
Duplicates
Latest version
Current behavior 😯
A server function called from an action (that is not the action itself) that return or throw a redirect to not redirect.
Works:
Does not work:
The latter works only if you manually propagate the response.
For example in an action:
But not when just using the response of the server function in other ways:
This means there is no consistent way to have redirects work from server functions.
Expected behavior 🤔
If a server function throws or returns a redirect then the calling action should respect that and propagate the redirect.
Steps to reproduce 🕹
Steps:
Context 🔦
No response
Your environment 🌎
No response
The text was updated successfully, but these errors were encountered: