SolidJSS
SolidJSโ€ข3y agoโ€ข
5 replies
ChrisThornham

"use server"; Help

I'm trying to understand how "use server"; works.

Is it safe to assume that everything under "use server"; runs on the server (of course scope dependent).

So, inside of the following function, only console.log(message); could run on the server.

const logHello = async (message: string) => {
  "use server";
  console.log(message);
};


However, when you call "use server"; at the top of a file, like in the
server.ts
file in the with-auth example, then all code runs on the server.

Excerpt from the
server.ts
file from the with-auth example project.
"use server";
import { redirect } from "@solidjs/router";
import { useSession } from "@solidjs/start/server";
import { getRequestEvent } from "solid-js/web";
import { db } from "./db";

function validateUsername(username: unknown) {
  if (typeof username !== "string" || username.length < 3) {
    return `Usernames must be at least 3 characters long`;
  }
}

function validatePassword(password: unknown) {
  if (typeof password !== "string" || password.length < 6) {
    return `Passwords must be at least 6 characters long`;
  }
}


I'm asking because the docs say...

"To create a function that only runs on the server, pass a function as a parameter to server$."

But I don't see any usage of server$ in the example projects.

Thanks,

Chris
Was this page helpful?