Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug?]: API routes do not import objects properly from "use server" files. #1283

Closed
2 tasks done
elliotwaite opened this issue Jan 25, 2024 · 4 comments
Closed
2 tasks done
Labels
bug Something isn't working

Comments

@elliotwaite
Copy link

elliotwaite commented Jan 25, 2024

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

If I have an API route that imports an object from a file that has "use server" at the top of it, it does not properly import the object and instead imports an empty object.

Demo on StackBlitz here.

Is this a bug, or am I using "use server" incorrectly? I thought that adding "use server" to the top of the file meant that the contents of that file would only be available server-side. And since API routes only run server-side, I thought they would still be able to import from those files. But when I add "user server" to the top of the files I'm importing from, it seems to break things.

Expected behavior 🤔

For API routes to be able to import from files that have "use server" at the top of them.

Steps to reproduce 🕹

Steps:

  1. Open this StackBlitz demo.
  2. Run pnpm dev
  3. Click the different links to see the different returned values from the API routes.
@elliotwaite elliotwaite added the bug Something isn't working label Jan 25, 2024
@lxsmnsyc
Copy link
Member

Must be a bundling issue

@ryansolid
Copy link
Member

"use server" top of the file means these are RPC functions that are exported so any import in the client will call them as fetch requests. It is not a general mechanism to say things are server only. I imagine it is an empty object because we wrap every export with a proxy that is looking for specific properties. So in a sense I guess it might be working if I had the proxy do passthrough. However, it is misleading because it isn't guaranteed to be server only. While it might be hard for us to determine this at compile time I think we should probably error at runtime on the server if we notice that what we get isn't a function.

@ryansolid
Copy link
Member

I've added an error to the server registration of server functions which should help make this clearer. So we need better docs in general. But I'm closing this by design.

@ryansolid ryansolid closed this as not planned Won't fix, can't repro, duplicate, stale Jan 25, 2024
@elliotwaite
Copy link
Author

Ah, I see. I was misunderstanding what "use server" did. Thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants