I just have a question about the behaviour of "use server" for a single function vs "use server" for a whole file.. I have a sign-up component that imports an action from my api:
Here is a working version of said action (note that this is the entire contents of the file) :
import { action } from "@solidjs/router";import { supabase } from "~/supabase/client";export const userSignUp = action(async (formData: FormData) => { "use server"; const email = formData.get("email"); const password = formData.get("password"); if (!email || !password) { throw new Error("Email and password are required"); } const { data, error } = await supabase.auth.signUp({ email: email as string, password: password as string, }); if (error) { throw new Error(error.message); } return data;}, "userSignUp");
import { action } from "@solidjs/router";import { supabase } from "~/supabase/client";export const userSignUp = action(async (formData: FormData) => { "use server"; const email = formData.get("email"); const password = formData.get("password"); if (!email || !password) { throw new Error("Email and password are required"); } const { data, error } = await supabase.auth.signUp({ email: email as string, password: password as string, }); if (error) { throw new Error(error.message); } return data;}, "userSignUp");
What doesn't work, is when I move the "use server" directive to the top of the file instead of for just the function. When I do this, I'm redirected to
https://action/userSignUp
https://action/userSignUp
(see attached image).
The "use server" documentation page doesn't seem to answer why this would be happening. Is this expected behaviour? And if so, could someone please explain to me why this is happening so I can better understand it?