Where exactly would I check the value of that header?
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.
redirectredirectredirectredirectredirectcachecacheactionloadload[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_async(admin).tsxnullthrow redirect("/login")///login/loginthrowreturnLocationLocationnativeEvent/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: /// 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("/");
};// 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;
}// routes/admin.tsx
import { load } from "~/lib/api";
export const route = {
load,
};
export default function Admin() {
return (
<main>
<h1>Admin</h1>
</main>
);
}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;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