How do you export Server Actions from a ts file?
Unanswered
Dogue Brasileiro posted this in #help-forum
Dogue BrasileiroOP
How do you export server actions from an external library?
This is my file structure:
How do i import the server-actions from lib to my nextjs playground ? It seems like normal import/export doesn't work for 'use server'.
This is my file structure:
lib/
src/
server-actions.ts
index.ts
custom-provider.tsx
playground
(contains nextjs app)
How do i import the server-actions from lib to my nextjs playground ? It seems like normal import/export doesn't work for 'use server'.
20 Replies
@Baird's Sparrow wdym?
Dogue BrasileiroOP
It seems like you can't import it from a external library
@Dogue Brasileiro It seems like you can't import it from a external library
Baird's Sparrow
@Dogue Brasileiro wdym?
Baird's Sparrow
i'm just saying export the server code, like the main actions performed by that library (without nextjs specific stuff)
and re-export the rest in a actions.ts file in your nextjs app
@Baird's Sparrow i'm just saying export the server code, like the main actions performed by that library (without nextjs specific stuff)
Dogue BrasileiroOP
And how do you make sure that the functions are not accidentally used on the client side by the users?
Server actions are meant to be used on the client, what do you mean?
@LuisLl Server actions are meant to be used on the client, what do you mean?
Rose-breasted Grosbeak
Not quite...
They expose an endpoint. The code is run on the server
They expose an endpoint. The code is run on the server
No I didn’t mean that, I said they’re meant to be used on the client not that they run on the client
Rose-breasted Grosbeak
Ah my bad, I thought OP is concerned about server actions being leaked to the client
Which is true for exported functions
Which is true for exported functions
@Dogue Brasileiro And how do you make sure that the functions are not accidentally used on the client side by the users?
Blood cockle
you can mark classes as private/protected in typescript.
https://www.typescriptlang.org/docs/handbook/2/classes.html#private
https://www.typescriptlang.org/docs/handbook/2/classes.html#private
Rose-breasted Grosbeak
What do classes have to do with server functions?
@LuisLl Server actions are meant to be used on the client, what do you mean?
Dogue BrasileiroOP
I am building an external library that have server actions
It seems like i can't use 'use server' directly in the files and bundle them. tsup doesn't allow this i think.
the next option would be to just export them as normal functions
but library users can use the functions in client components directly
Dogue BrasileiroOP
it would have been great if i could just export the server actions directly. And users could import them without having to define it in their actions.ts file