UI Elements stop working w/ beforeLoad
I'm encountering a strange bug where if I call getCookie in my route, the UI elements (in this case tabs) don't work.
Here is the code:
If I uncomment getCookie("userId"), suddenly my UI is not interactable anymore and I get this console error:
7 Replies
national-gold•4mo ago
what is
getCookie ?adverse-sapphireOP•4mo ago
import { getCookie, setCookie } from "@tanstack/react-start/server";
national-gold•4mo ago
you cannot call server stuff in beforeLoad since this runs on the client as well
if you want to execute some function on the client always, then wrap in into a server function
adverse-sapphireOP•4mo ago
I see well I want to execute this function on the server to redirect/route clients to specific pages (via createFileRoute) if they have a cookie set.
so where would be the best place to put this?
national-gold•4mo ago
wrap it in a server function then and execute it in beforeLoad
but be aware that this executes for each navigation
so you might want to cache it
adverse-sapphireOP•4mo ago
would I cache it via this:
createServerFnStaticCache?
I also was looking into middlewares but couldn't figure out how to execute a middleware call into the createFileRoute functionnational-gold•4mo ago
you could just use query for this