SolidStart - Server Only

I see a way to ensure functions only run on a server, but what about modules that declare classes? I guess that I need to use a factory function instead? https://docs.solidjs.com/solid-start/reference/server/use-server
6 Replies
Brendonovich
Brendonovich3mo ago
If you only import those modules in places that are use server then they won't reach the client If you want to ensure a module isn't imported on the client add
import { isServer } from "solid-js/web";
if (!isServer) throw new Error("This module is server only");
import { isServer } from "solid-js/web";
if (!isServer) throw new Error("This module is server only");
jrainearwills
jrainearwillsOP3mo ago
yeah, but what if someone mistakenly uses a module that i don't want to ever reach the client? how do i say to the next developer or to myself "Hey don't use this on the client side!" better than nothing i guess. the lines are a little blurred here. it makes me queezy.
Brendonovich
Brendonovich3mo ago
Also to be a bit more precise use server shouldn't just be used everywhere that you want to keep something server-only. It should only be used for marking functions that you want to expose to the client via RPC fwiw react's equivalent is to import a server-only npm package that has basically that same code inside of it
jrainearwills
jrainearwillsOP3mo ago
i'm used to making separate web services, but i like the convenience of solidstart. afik the only reason to leverage "use server" is for convenience. is there another?
Brendonovich
Brendonovich3mo ago
Only other thing i can think of is that they support Single Flight Mutations It is mostly about convenience - being able to define server things inline and having serialization of a bunch of types dealt with for you
jrainearwills
jrainearwillsOP3mo ago
FWIW I was able to use a a factory function in combination with "use server" like:
"use server"

function SuperSecret(){
return {
doThatThingWithSecrets: () => {
console.log("shhhhh")
}
}
}

export const SuperSecretCode = SuperSecret()
"use server"

function SuperSecret(){
return {
doThatThingWithSecrets: () => {
console.log("shhhhh")
}
}
}

export const SuperSecretCode = SuperSecret()
Want results from more Discord servers?
Add your server