Closed jgeschwendt closed 1 week ago
Could you try one thing, move the use server
directive, to the beginning of the function. Say, here:
export async function setValue(string: string): Promise<void> {
'use server';
// comment out the next two statements to successfully compile
Why? Well, it has been a while now, but IIRC, relay, pre-processes the file, and adds more code to it, that might be pushing down the use server
directive at the top:
// for example after relay processes the file we might have
import someRelayStuff from './folder-made-by-the-relay-compiler';
// more of those
'use server';
import { revalidatePath } from "next/cache";
And that causes the issue you are seeing. We can try to confirm this by placing the use server
directive within the function, which is also an allowed pattern.
moving the directive inside the function results in this error:
jgeschwendt@repro-next-relay-server-action % pnpm build
> repro-next-relay-server-action@0.1.0 build /Users/jgeschwendt/GitHub/jgeschwendt/repro-next-relay-server-action
> next build
▲ Next.js 14.2.3
Creating an optimized production build ...
Failed to compile.
./src/app/actions.ts
Error:
x It is not allowed to define inline "use server" annotated Server Actions in Client Components.
| To use Server Actions in a Client Component, you can either export them from a separate file with "use server" at the top, or pass them down through props from a Server Component.
|
| Read more: https://nextjs.org/docs/app/api-reference/functions/server-actions#with-client-components
|
,-[/Users/jgeschwendt/GitHub/jgeschwendt/repro-next-relay-server-action/src/app/actions.ts:3:1]
3 | import type { actionsSetValueMutation } from '@/relay/actionsSetValueMutation.graphql';
4 |
5 | export async function setValue(string: string): Promise<void> {
6 | "use server";
: ^^^^^^^^^^^^
7 |
8 | // comment out the next two statements to successfully compile
9 | const taggedNode = graphql`
`----
Import trace for requested module:
./src/app/actions.ts
./src/app/Interactive.tsx
> Build failed because of webpack errors
Not on my laptop now, but are you importing this function directly into a client module (use client)? If so, import it to a server component instead and pass it down as props, just to test if all of this makes it work... We are exploring the issue.
my bad, yes moving it into the RSC works https://github.com/jgeschwendt/repro-next-relay-server-action/blob/rsc-action/src/app/page.tsx
Alright, but then maybe this is something that Relay should fix, as in, respect, or check for, the presence of use server
on the file. I'd say maybe open an issue over there, referencing this one.
It's a bug of SWC relay plugin
~It was not the case. The SWC Wasm plugin was correct.~
eagerEsModules: true,
was the cause
https://github.com/swc-project/plugins/pull/306 should fix it
Link to the code that reproduces this issue
https://github.com/jgeschwendt/repro-next-relay-server-action
To Reproduce
using the following relay code in a server action:
https://github.com/jgeschwendt/repro-next-relay-server-action/blob/test/src/app/actions.ts#L8-L16
causes this error:
reproduced in GitHub Actions: https://github.com/jgeschwendt/repro-next-relay-server-action/actions/workflows/ci.yaml
Current vs. Expected behavior
I am able to use the relay compiled tagged node in a server action
Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure, SWC
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local)
Additional context
No response