Server file being bundled on the frontend and env variable is undefined when used via process.env

I am facing an issue in solid start where my server file marked as "use server" is being bundled with the frontend code and I am unable to access node/server related code and getting errors in the chrome console. For testing this issue I created a basic "bare" solid start project. Created a src/lib folder with 2 files server.ts and index.ts (image attached). app.confg.ts (image attached as well). The error that I get in the console is "TEST_ENV_VARIABLE is not set" as the proces.env.TEST_ENV_VARIABLE is undefined. Also if I comment this error check and console the count value on the frontend then I get this error (see attached image). All these errors are due to server code being bundlede on the frontend.
No description
No description
No description
8 Replies
Madaxen86
Madaxen862mo ago
As solid-start is moving on from Vinxi - the server functions of vinxi have been replaced with the ones from Tanstack start. At the moment top level "use server" is not supported. Plus keep in mind that "use server" means call function on the server, replace with fetch request in the client. There will still be a reference to the function in the client bundle. If you really want a function to be only available in the server and should throw when called on the client you can add this to the file where it’s exported
if(!isServer) throw Error("function… can only be called on the server");
if(!isServer) throw Error("function… can only be called on the server");
Regarding .env : solid-start does not add env variables to process.env in production by default. You’ll need to install a library for that. Personally I use dotenv-cli And change the package.json scripts to "dev":"dotenv vinxi dev", "build":"dotenv vinxi build", "start":"dotenv vinxi start" …
Adam Goldstein
Adam Goldstein2mo ago
Is the plan to bring back support for top-level "use server"? The docs still note it as an option, this is a pretty significant breaking change for a minor semver increment. We might just have to stay on the old version for the time being
Brendonovich
Brendonovich2mo ago
Top level ‘use server’ does work, my projects would be in trouble if it didn’t haha This seems like the error from the server is making its way to the frontend, rather than server code being bundled to the client. That’s expected behaviour. Use something like dotenv as shown above to load the env var on the server
ellers
ellers2mo ago
I thought env vars worked without additional dependencies, but had to be prefixed with VITE_ to make it into the bundle. Could be wrong.
Madaxen86
Madaxen862mo ago
Prefix VITE_ is only for public env vars - never use them for secrets like keys! Otherwise you’ll leak secrets to the client bundle and that’s a major security issue.
ellers
ellers2mo ago
agreed, and fair point to make that clear 💯
Adam Goldstein
@Brendonovich I'm working with @Syed Samar on this, and I can confirm from looking at network requests/the debugger that unfortunately the server code is being bundled in the client. Most notably, when lines 5-10 of server.ts are moved inside the queryWorksheets function, there is no error, it works fine that way. It seems to be a bug with the new implementation of top level "use server" for any functionality running at a top level outside of functions. Is this degradation tracked anywhere, either in docs or as a failing test intended to be fixed in a future release? Are there any possible workarounds with config in the meantime, besides massive code refactors? We have a large Solid Start codebase which is unable to be updated to the latest version because of this
Brendonovich
Brendonovich5w ago
Is this in dev or the prod build? I'm not sure anyone else has reported this, would you mind making a small reproduction of it?

Did you find this page helpful?