TanStackT
TanStack11mo ago
3 replies
dead-brown

Ensure server-only code remains server-only

having a bit of trouble understanding how "server only" code stays "server only" in TSS. i assume this dynamic import is an anti pattern:

export const listOrganizations = async () => {
  const auth = await import("@/lib/server/auth").then((mod) => mod.auth);
  const req = getWebRequest();
  if (!req) {
    throw new Error("No request found");
  }
  const orgs = await auth.api.listOrganizations({ headers: req.headers });
  return orgs;
};


i'm not sure how i'd use that function in a
beforeLoad
otherwise, eg



import { listOrganizations } from "@/lib/orgs";

export const Route = createFileRoute("/(app)/_app")({
  component: RouteComponent,
  beforeLoad: async ({ context }) => {
    if (!context.user) {
      throw redirect({
        to: "/signin",
      });
    }
    const orgs = await listOrganizations()
    console.log(`orgs: ${JSON.stringify(orgs)}`)
  },
});


any help is much appreciated!
Was this page helpful?