Closed maksimsemenov closed 1 week ago
So a couple things you need to do. You need to return instead of throw the redirect. Secondly you need to wrap in the cache function. Also you need to call the function in createAsync()
const loadData = cache(() => {
'use server'
return redirect('/about')
}, "load-data");
export const route = {
load: () => loadData()
}
export default function Home() {
const data = createAsync(() => loadData());
}
Throwing the redirect also works, but the important part is calling loadData
outside of the load
function, since thrown/returned redirects will be ignored when called from load
.
https://stackblitz.com/edit/github-fk8exj-mcht9p?file=src%2Froutes%2Findex.tsx
I feel this is intended behavior at the moment. Thinking we can probably close this issue?
What if I don't want to cache the request? For example, if I want to validate some auth-link with some hash parameter, and then want to redirect user. I don't think I want to cache the response in this case.
Use action
, it's for pretty much everything that isn't a data fetch
Can I use action
on page load? The use-case I'm trying to solve is:
I also, don't want to cash that response, so if link is clicked twice, it is no longer valid
Yeah that should work fine
So should it be like this:
const loadData = action(() => {
'use server'
return redirect('/about')
});
export const route = {
load: () => loadData()
}
export default function Home() {
const data = createAsync(() => loadData());
}
Yeah this is by design since the load
function is more of like a lifecycle hook and the server function is just an async function. One could catch the errored response themselves and then call the client side navigation APIs with it but I don't know if that is what you are going for. The name cache
is probably misleading because on the server it only lasts the lifetime of the request. It is possible it would just work. We're reviewing some of the naming.
I think an action is fine, but I think cache
more fits the semantics because it is getting is validated on a hash. But either probably is fine here.
Duplicates
Latest version
Current behavior 😯
I'm trying to throw a redirect in the route load function:
When I try to navigate to that page, instead of redirecting, it throws a server error:
Expected behavior 🤔
It should redirect me to the page in the redirect
Steps to reproduce 🕹
I created a repository with an example: https://github.com/maksimsemenov/solid-start-redirect?tab=readme-ov-file
Steps to reproduce:
/
route.Context 🔦
I initiated project with
npm init solid@latest
.I also tried just to return
redirect
instead of throwing it. In this case there is no server error, but no redirect either.I found a few similar issues that are are resolved. But it seems that the issue is back
Your environment 🌎