Giter Site home page Giter Site logo

Comments (6)

kdy1 avatar kdy1 commented on June 2, 2024 2

It's a bug of SWC relay plugin

from next.js.

icyJoseph avatar icyJoseph commented on June 2, 2024

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.

from next.js.

jgeschwendt avatar jgeschwendt commented on June 2, 2024

moving the directive inside the function results in this error:

jgeschwendt@repro-next-relay-server-action % pnpm build

> [email protected] 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

https://github.com/jgeschwendt/repro-next-relay-server-action/actions/runs/9099994826/job/25013834155

from next.js.

icyJoseph avatar icyJoseph commented on June 2, 2024

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.

from next.js.

jgeschwendt avatar jgeschwendt commented on June 2, 2024

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

from next.js.

icyJoseph avatar icyJoseph commented on June 2, 2024

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.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.