S
SolidJS3mo ago
ensi

Cannot call server function outside of a request

Tried to initialize Supertokens backend client in parallel with main router, but got this error: Cannot call server function outside of a request Supertokens client initializer:
"use server";

import supertokens from "supertokens-node";
import Session from "supertokens-node/recipe/session";
import EmailPassword from "supertokens-node/recipe/emailpassword";

export const initAuthServer = () => {
supertokens.init({
framework: "express",
supertokens: {
connectionURI:
"https://st-dev-5b734821-6414-11ef-8c62-03ca6dc7cc97.aws.supertokens.io",
apiKey: "a9ntJvmIYaQ-iWRUsLnGRDsWV-",
},
appInfo: {
appName: "ficlib",
apiDomain: "http://localhost:8000",
websiteDomain: "http://localhost:3000",
apiBasePath: "/auth",
websiteBasePath: "/auth",
},
recipeList: [EmailPassword.init(), Session.init()],
});
};
"use server";

import supertokens from "supertokens-node";
import Session from "supertokens-node/recipe/session";
import EmailPassword from "supertokens-node/recipe/emailpassword";

export const initAuthServer = () => {
supertokens.init({
framework: "express",
supertokens: {
connectionURI:
"https://st-dev-5b734821-6414-11ef-8c62-03ca6dc7cc97.aws.supertokens.io",
apiKey: "a9ntJvmIYaQ-iWRUsLnGRDsWV-",
},
appInfo: {
appName: "ficlib",
apiDomain: "http://localhost:8000",
websiteDomain: "http://localhost:3000",
apiBasePath: "/auth",
websiteBasePath: "/auth",
},
recipeList: [EmailPassword.init(), Session.init()],
});
};
entry-server:
import { createHandler, StartServer } from "@solidjs/start/server";
import { initAuthServer } from "./app/auth/server";

initAuthServer(); // usage

export default createHandler(() => (
<StartServer
document={({ assets, children, scripts }) => (
<html lang="en" class="dark">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1"
/>
{assets}
</head>
<body>
<div id="app">{children}</div>
{scripts}
</body>
</html>
)}
/>
));
import { createHandler, StartServer } from "@solidjs/start/server";
import { initAuthServer } from "./app/auth/server";

initAuthServer(); // usage

export default createHandler(() => (
<StartServer
document={({ assets, children, scripts }) => (
<html lang="en" class="dark">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1"
/>
{assets}
</head>
<body>
<div id="app">{children}</div>
{scripts}
</body>
</html>
)}
/>
));
I didn't fully understand how to segregate server-side code.
No description
9 Replies
Brendonovich
Brendonovich3mo ago
You probably don’t want use server on that function anyway since it’s not something you’d call on the client Only put use server on things you want to turn into api endpoints
ensi
ensiOP3mo ago
so it won't leak in client if I import this module only in entry-server?
Brendonovich
Brendonovich3mo ago
Yeah You can make sure of it by throwing an error in the module if !isServer
ensi
ensiOP3mo ago
looks boilerplaty but ok
Brendonovich
Brendonovich3mo ago
It’s like 2 lines lol
ensi
ensiOP3mo ago
is there any trpc examples so I can understand how segregation works?
Brendonovich
Brendonovich3mo ago
I think there’s one in the repo
ensi
ensiOP3mo ago
oh ok, tysm
Brendonovich
Brendonovich3mo ago
I think with trpc you usually use an api route
Want results from more Discord servers?
Add your server