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

server actions on next@^14.1.1 doesn't transform/work correctly #63603

Closed
erfanium opened this issue Mar 22, 2024 · 6 comments · Fixed by #64398
Closed

server actions on next@^14.1.1 doesn't transform/work correctly #63603

erfanium opened this issue Mar 22, 2024 · 6 comments · Fixed by #64398
Assignees
Labels
bug Issue was opened via the bug report template. locked

Comments

@erfanium
Copy link
Contributor

erfanium commented Mar 22, 2024

Link to the code that reproduces this issue

https://github.com/erfanium/nextjs-actions-error-reproduction

To Reproduce

I made a reproduction repo for this issue: https://github.com/erfanium/nextjs-actions-error-reproduction. You can just start this app and click on the button on the page and see the unexpected error.

if you want to do it from scratch and not using my reproduction repo:

  1. create a server action by creating a file named actions.ts
"use server";

const defineAction = <T, U>({ cb }: { cb: (params: T) => Promise<U> }) => cb;

export const action = defineAction({
  async cb({ foo }: { foo: string }) {
    (() => {
      console.log(foo);
    })();
    return foo;
  },
});
  1. somehow call this action in a page
"use client";
import { action } from "./actions";
import styles from "./page.module.css";
export default function Home() {
  return (
    <main onClick={() => action({ foo: "hey" })} className={styles.main}>
      <button>Click on me and then see the console logs</button>
    </main>
  );
}
  1. see server console errors
 ⨯ src/app/actions.ts (8:18) @ foo
 ⨯ unhandledRejection: ReferenceError: foo is not defined
    at $$ACTION_0 (webpack-internal:///(action-browser)/./src/app/actions.ts:20:17)
    at cb (webpack-internal:///(action-browser)/./src/app/actions.ts:15:155)
    at endpoint (webpack-internal:///(action-browser)/./node_modules/next/dist/build/webpack/loaders/next-flight-action-entry-loader.js?actions=%5B%5B%22%2Fhome%2Ferfanium%2FDocuments%2Fnextjs-bug%2Fsrc%2Fapp%2Factions.ts%22%2C%5B%22action%22%2C%22%24%24ACTION_0%22%5D%5D%5D&__client_imported__=true!:9:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /home/erfanium/Documents/nextjs-bug/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:39:406
    at async t2 (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:38:6412)
    at async rS (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:41:1369)
    at async doRender (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/base-server.js:1378:30)
    at async cacheEntry.responseCache.get.routeKind (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/base-server.js:1539:28)
    at async DevServer.renderToResponseWithComponentsImpl (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/base-server.js:1447:28)
    at async DevServer.renderPageComponent (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/base-server.js:1844:24)
    at async DevServer.renderToResponseImpl (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/base-server.js:1882:32)
    at async DevServer.pipeImpl (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/base-server.js:895:25)
    at async NextNodeServer.handleCatchallRenderRequest (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/next-server.js:269:17)
    at async DevServer.handleRequestImpl (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/base-server.js:791:17)
    at async /home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/dev/next-dev-server.js:331:20
    at async Span.traceAsyncFn (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/trace/trace.js:151:20)
    at async DevServer.handleRequest (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/dev/next-dev-server.js:328:24)
    at async invokeRender (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/lib/router-server.js:377:13)
    at async Server.requestListener (/home/erfanium/Documents/nextjs-bug/node_modules/next/dist/server/lib/start-server.js:140:13)
   6 |   async cb({ foo }: { foo: string }) {
   7 |     (() => {
>  8 |       console.log(foo);
     |                  ^
   9 |     })();
  10 |     return foo;
  11 |   },

Current vs. Expected behavior

expected behavior is to this code should work without any error like older next versions (older than 14.1.2)

Provide environment information

this affected

Operating System:
  Platform: linux
  Arch: x64
  Version: #40-Ubuntu SMP PREEMPT_DYNAMIC Tue Nov 14 14:18:00 UTC 2023
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: 8.12.1
Relevant Packages:
  next: 14.2.0
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.3
Next.js Config:
  output: N/A

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

Not sure
this affected

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

next dev (local), next start (local)

Additional context

  1. next versions below 14.1.1 are working fine. 14.1.1 to current latest version (14.2.0) have this issue
  2. I use app directory
  3. I'm not sure if this affected Vercel or not. because i'm not a user of Vercel
@erfanium erfanium added the bug Issue was opened via the bug report template. label Mar 22, 2024
@erfanium erfanium changed the title server actions on next@^14.1.2 doesn't compile/work correctly server actions on next@^14.1.1 doesn't compile/work correctly Mar 22, 2024
@erfanium
Copy link
Contributor Author

maybe it's related to #61001

@erfanium erfanium changed the title server actions on next@^14.1.1 doesn't compile/work correctly server actions on next@^14.1.1 doesn't transform/work correctly Mar 22, 2024
@georgesmahl
Copy link

I have the same issue, server actions seem to return promises when they returned the promised object in the past? (my version is 14.1.4)

export async function createOrganization(name: string, description: string, logo: string) {
  const info = getInfo();

used to work, but now I need to do:

export async function createOrganization(name: string, description: string, logo: string) {
  const info = await getInfo();

@erfanium
Copy link
Contributor Author

this issue is still valid on next@14.2.0

@erfanium
Copy link
Contributor Author

Hey @shuding,
Can you check this? Is this behavior expected?

@shuding shuding self-assigned this Apr 12, 2024
@shuding
Copy link
Member

shuding commented Apr 12, 2024

I’ll take a look at the compilation issue!

@georgesmahl that is the expected behavior:

Because the underlying network calls are always asynchronous, 'use server' can only be used on async functions.
https://react.dev/reference/react/use-server

shuding added a commit that referenced this issue Apr 12, 2024
This PR fixes the case where object method and class method prop
functions are not considered as a new scope. Closes #63603.

Closes NEXT-3088
ztanner pushed a commit that referenced this issue Apr 17, 2024
This PR fixes the case where object method and class method prop
functions are not considered as a new scope. Closes #63603.

Closes NEXT-3088
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 Apr 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants