Hi guys,
Is it possible to debug a server function? And if so, how?
Cheers!
4 Replies
like-gold•4mo ago
Can you share more info? What are you attempting to do?
Also I don't see a reason you can't add a breakpoint, just be aware what happens in the server func is server side (it gets extracted)
xenial-blackOP•4mo ago
Thank you for the response. I should mention that this is one of my first approaches with Start, and I was wondering what the approach would be to test a server function. I imagined that, being server-side, I wouldn't be able to debug using Chrome DevTools. I'm attaching code for completeness: import * as fs from "node:fs";
import { createFileRoute, useRouter } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";
import { getCookie, getEvent } from "@tanstack/react-start/server";
const retrieveUserSession = createServerFn({
method: "GET",
}).handler(async () => {
const event = getEvent();
const cookie = getCookie(event, "test_cookie");
debugger;
return cookie ? { cookie } : undefined;
});
export const Route = createFileRoute("/")({
component: Home,
loader: async () => {
const session = await retrieveUserSession();
return { session };
},
});
function Home() {
const router = useRouter();
const { session } = Route.useLoaderData();
return <span>Hello world: {session?.cookie}</span>;
}
flat-fuchsia•4mo ago
Correct chrome dev-tools will not work for server side code. If you add console logs to your server func, they will appear into the console you started your app in. For proper step debugging, you would want to use the debug funcitonality of your IDE. Then you can set breakpoints within the server function.
like-gold•4mo ago
you can use chrome devtools to hook to a nodejs process (only backend though) FYI but its usually just easier to use IDE