redirect is for use within cache/action, try extracting that async function into a cache and calling it in loadasync by extension redirects won't work.null for the user object, but doesn’t redirect./ but that doesn't work with redirecting from /login to / if you're already logged inthrow and return, neither works/login needs to be in the header. Two paths:load tried to access the response header when it was already too late. redirecting (on the server side) outside of where the server's response can be touched or something else is modifying the header later?redirect, the navigation to that route will comply with the redirect as well.Where exactly would I check the value of that header?
redirect.As soon as you add a direct dependency in the route component to a cache point that just threw a redirect, the navigation to that route will comply with the redirect as well.
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#<_Response>".] {
code: 'ERR_UNHANDLED_REJECTION'
}return redirect/server_(admin).tsxthrow redirect("/login")LocationLocationnativeEvent/manage-ordersUncaught (in promise) Response {type: 'default', url: '', redirected: false, status: 302, ok: false, …}http://localhost:3000/_server/?id=…%2Fsrc%2Froutes%2F(admin).tsx%3Fpick%3Droute&name=%24%24function0manage-ordergetCurrentUser()HeadersLocation: /// routes/admin.tsx
import { load } from "~/lib/api";
export const route = {
load,
};
export default function Admin() {
return (
<main>
<h1>Admin</h1>
</main>
);
}// lib/server.ts
export const load = async () => {
"use server";
const cookie = getCookie("chat_session");
if (!cookie) throw redirect("/");
};// lib/api.ts
import { action, cache } from "@solidjs/router";
import {
getChat as _getChat,
createChat as _createChat,
sendMessage as _sendMessage,
load as _load,
} from "./server";
export const getChat = cache(_getChat, "chat");
export const createChatAction = action(_createChat, "create-chat");
export const sendMessage = action(_sendMessage, "send-message");
export const load = cache(_load, "load");// In a server module
async function hasChatCookie() {
'use server';
return getCookie('chat_session') !== undefined;
}
// in a client module
const load = cache(async () => {
const result = await hasChatCookie();
if (!result) throw redirect('/');
return true;
}, 'load');
// in the route module
export const route = {
load: async () => load(),
};async () => {
"use server";
const cookie = getCookie("chat_session");
if (!cookie) throw redirect("/");
};const getCurrentUser = cache(function () {
"use server";
const event = getRequestEvent()!;
if (!event.locals.user) {
console.log(event.locals.user);
return redirect("/login");
}
return event.locals.user;
}, "current_user");
export const route = {
load() {
void getCurrentUser();
},
} satisfies RouteDefinition;// file: src/routes/(admin).tsx
import {
cache,
createAsync,
redirect,
type RouteDefinition,
type RouteSectionProps,
} from '@solidjs/router';
import { getRequestEvent } from 'solid-js/web';
const getCurrentUser = cache(async function () {
'use server';
const event = getRequestEvent()!;
if (!event.locals.user) {
throw redirect('/');
}
return event.locals.user;
}, 'current_user');
export const route = {
load() {
void getCurrentUser();
},
} satisfies RouteDefinition;
export default function Admin(props: RouteSectionProps) {
// Add this line and it prevents the navigation
// (complies with the redirect?) when there is no user.
const _user = createAsync(() => getCurrentUser());
return props.children;
}HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
location: /
content-type: text/javascript
Date: Tue, 28 May 2024 15:31:21 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Transfer-Encoding: chunked