Redirect in createResource

Hey, I don't quite understand where to use redirect, thought it was meant to run server-side but doesnt appear to work inside a deferred createResource. What do I need to do to make this work?
import { redirect } from '@solidjs/router';
import { createResource } from 'solid-js';

export function handleRedirect() {
'use server';

throw redirect('/404');
}

export default function CatchAll() {
createResource(handleRedirect, { deferStream: true });

return (<></>);
}
import { redirect } from '@solidjs/router';
import { createResource } from 'solid-js';

export function handleRedirect() {
'use server';

throw redirect('/404');
}

export default function CatchAll() {
createResource(handleRedirect, { deferStream: true });

return (<></>);
}
16 Replies
brenelz
brenelz3w ago
Does it work if you wrap the handle redirect function in a query()?
Christian
ChristianOP3w ago
Do you know when that was added? Currently on solid-start 1.0.5 and solid 1.8.18. I didn't see it popping up as an import when I tried. I tried to update earlier this week but was facing hydration errors and just ended up reverting.
brenelz
brenelz3w ago
It might be called "cache" instead then. It was renamed
Christian
ChristianOP3w ago
Let me try
import { cache, createAsync, redirect } from '@solidjs/router';

const handleRedirect = cache(async () => {
'use server';

throw redirect('/404');
}, '');

export default function CatchAll() {
createAsync(() => handleRedirect(), { deferStream: true });

return (<></>);
}
import { cache, createAsync, redirect } from '@solidjs/router';

const handleRedirect = cache(async () => {
'use server';

throw redirect('/404');
}, '');

export default function CatchAll() {
createAsync(() => handleRedirect(), { deferStream: true });

return (<></>);
}
Got it to work when swapping to cache and createAsync. Thanks @brenelz
brenelz
brenelz3w ago
Oh ok... So it probably doesn't work with resources
Christian
ChristianOP3w ago
Might with cache but I swapped to createAsync as I was reading some other documentation, I'll try with createResource in a moment. Works with create resource as well, not sure it matters here either way since it's just a throwaway to redirect
brenelz
brenelz3w ago
I thought maybe you had to use the signal from createAsync but I guess not
Christian
ChristianOP3w ago
Yeah unsure, do you happen to know why it requires a cache/query for this by the way? Bit confused why it can't just run in a SSRed function
brenelz
brenelz3w ago
Also you might be able to use the navigate component if you just need to redirect https://docs.solidjs.com/solid-router/reference/components/navigate
Christian
ChristianOP3w ago
Navigate runs on client though correct? This is just for a catch-all route to redirect to /404 if they go to a unspecified or protected route. Guess it doesn't really matter too much if it's client vs ssr in fairness, but I just preferred it on the server.
brenelz
brenelz3w ago
Yeah not sure
Christian
ChristianOP3w ago
No worries, regardless thanks for your help on solving it.
brenelz
brenelz3w ago
I think the cache wrapper is the one that intercepts and handles the redirect
Christian
ChristianOP3w ago
Ahh interesting. I might go look into the internals of it later
brenelz
brenelz3w ago
There is also a built in 404 catch all route in solidstart?
Christian
ChristianOP3w ago
I wanted it to redirect though. I use the catchall with "*404" then redirect to /404
Want results from more Discord servers?
Add your server