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

A "use server" file can only export async functions' error when exporting non-async/cache functions #62926

Closed
1 task done
RiodeJaneiroo opened this issue Mar 6, 2024 · 6 comments
Labels
examples Issue/PR related to examples locked

Comments

@RiodeJaneiroo
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:27 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T8103
  Available memory (MB): 8192
  Available CPU cores: 8
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: 1.22.21
  pnpm: 8.15.4
Relevant Packages:
  next: 14.2.0-canary.2 // Latest available version is detected (14.2.0-canary.2).
  eslint-config-next: 14.1.2
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3
Next.js Config:
  output: standalone

Which example does this report relate to?

none

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

When using the "use server" directive at the top of a file in a Next.js application, if the file exports a non-async function or a React cache function (e.g., React.cache), it throws the following error:

Error: A "use server" file can only export async functions.

Expected Behavior

The expected behavior is to allow exporting non-async functions and React cache functions from a file with the "use server" directive without any errors.

To Reproduce

StackBlitz

@RiodeJaneiroo RiodeJaneiroo added the examples Issue/PR related to examples label Mar 6, 2024
@RiodeJaneiroo RiodeJaneiroo changed the title "use server" with cache A "use server" file can only export async functions' error when exporting non-async/cache functions Mar 6, 2024
@chungweileong94
Copy link
Contributor

The issue is caused by the stricter use server check in the recent 14.1.2 update, it seems to me NextJS is performing syntax-level checking which might raise some false errors. It actually breaks my server action builder package.

Repro: https://github.com/chungweileong94/next-use-server-bug

@ceIia
Copy link

ceIia commented Mar 6, 2024

yeah, i had to make sure i used the export async function(return somepromise) syntax, as export const = async () => { await ... } would not work

@chungweileong94
Copy link
Contributor

chungweileong94 commented Mar 7, 2024

It seems like NextJS is doing action.constructor.name !== 'AsyncFunction' check, however, if the action is created via a library, it will get transpiled into Promise<...> instead of action.constructor.name !== 'AsyncFunction'. The only workaround now is to create a async function wrapper around, which is kinda annoying.

// Note: if it's from library code with `async` being transpiled to returning a promise,

@bduff9
Copy link

bduff9 commented Mar 7, 2024

Its worth noting that this currently breaks the example shown in the documentation around using React.cache currently: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-server-with-third-party-libraries

@shuding
Copy link
Member

shuding commented Mar 10, 2024

This is a duplicate of #62860 - please give #62860 (comment) and #62821 a read. We'll keep everyone updated there.

Basically it does not make sense to use React.cache together with Server Actions and it will not cache anything.

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples locked
Projects
None yet
Development

No branches or pull requests

5 participants