Skip to content

Server actions doesn't work with hoisted function declarations #57392

Open
@sophiebits

Description

@sophiebits

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/nice-babbage-hvkm9v?file=%2Fapp%2Fpage.tsx

To Reproduce

  1. Have a "use server" function declaration appear in the file later than where it is used, and after the return.

Current vs. Expected behavior

This should work because it would work in normal JS. Instead, the createActionProxy call is inserted after the use and after the return statement.

Do note that the captured variables should not be read at the time the action is referenced but rather (I believe) their final value, which seems the closest thing to when they are called. eg

function Test() {
  let b = <button onClick={foo} />;
  let a = 2;
  return b;

  async function foo() {
    'use server';
    doSomethingWith(a);
  }
}

should presumably call doSomethingWith(2).

Verify canary release

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

Provide environment information

(didn't test on latest canary because I couldn't get codesandbox to run it, but I read the commit log of packages/next-swc/crates/core/src/server_actions.rs since 13.5.6)

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
Binaries:
  Node: 16.17.0
  npm: 8.15.0
  Yarn: 1.22.19
  pnpm: 7.1.0
Relevant Packages:
  next: 13.5.6
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

No response

Metadata

Metadata

Assignees

Labels

bugIssue was opened via the bug report template.locked

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions