Giter Site home page Giter Site logo

Server action called from client component is invoked but return data is always undefined (even though network response has data in it) about next.js HOT 16 CLOSED

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024
Server action called from client component is invoked but return data is always undefined (even though network response has data in it)

from next.js.

Comments (16)

caioalmeida12 avatar caioalmeida12 commented on September 13, 2024 1

Hello, Rahul.

Just tried to run your code and, apparently, the error does not occur on my machine (i dont know why)

image

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024 1

@ztanner Works on my project, thanks a lot!

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024

@caioalmeida12 That's because you ran localhost:3000/test without adding the subdomain in front. As per my reproduction steps you need to navigate to demo.localhost:3000/test. I will update the repo to return 404s without subdomain

from next.js.

dfltr avatar dfltr commented on September 13, 2024

Check your network panel for a CORS error. Having a request/response mysteriously zero itself out is a smell that usually points to a CORS violation, as the browser will ditch the body of the request without any other indication that something went wrong.

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024

@dfltr Route handlers work though and don't see anything, does the code work when you run it locally?

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024

Update on this: regular server actions used in

elements return undefined as well even though the action is being performed. This is a pretty critical bug hope it can be resolved or touched upon

from next.js.

ztanner avatar ztanner commented on September 13, 2024

It looks like your middleware is rewriting the IPC call that powers the server action on the /test page. The fetch call associated with your server action is only bundled with the /[domain]/test route and it's being rewritten to /localhost:3000/[domain]/test.

I'm not 100% following your middleware logic but you can avoid rewriting the action handlers by ensuring the rewrite isn't called for requests that contain the next-action header.

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024

Ahhh that makes sense, thank you @ztanner for the quick response!

What Im trying to do in the middleware is take any subdomain request like test.localhost:3000/ and in the middleware add the domain as a slug that I can use to support multi tenancy. Majority of this middleware code is from the Vercel Platforms Starter Kit. I think this is something that needs to be addressed in the starter kit then since server actions come undefined there as well.

Would there be another way you'd suggest to go about this?

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024

Hey @ztanner, can't get redirects within server actions to work even though Im excluding it on the middleware. If I put an absolute url in the redirect as well it'll show a 404 and then upon refresh go to the right place :/ It seems like redirects also include the next-action header so when I exclude it, it doesn't do the redirection properly.

from next.js.

ztanner avatar ztanner commented on September 13, 2024

Hey @RahulBirCodes -- I took a closer look and realized the rewrite actually isn't the inherent problem, so you can ignore that suggestion. I think what's happening is that the middleware is causing the content-type header that we would normally set on the action to get lost on the response. Could you try something like this in your middleware and let me know if it works for you?

  if (req.headers.get('next-action')) {
    newHeaders.set('content-type', 'text/x-component')
  }

(I don't expect this to be something you have to worry about in your code, just wanting to identify the root cause)

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024

Hey @ztanner, just tested and got the following error on the client: ⨯ SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2)
at JSON.parse ()
digest: "837905482"

from next.js.

ztanner avatar ztanner commented on September 13, 2024

Hey @ztanner, just tested and got the following error on the client: ⨯ SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2) at JSON.parse () digest: "837905482"

Does that happen in your repro, or do you have different code that's causing that behavior? If so could you update your repro?

from next.js.

RahulBirCodes avatar RahulBirCodes commented on September 13, 2024

@ztanner just pushed to my repro. It's throwing the json error as well now. i updated it to mimic my actual project a little bit so when you're testing go to demo.localhost:3000/login instead.

from next.js.

ztanner avatar ztanner commented on September 13, 2024

@ztanner just pushed to my repro. It's throwing the json error as well now. i updated it to mimic my actual project a little bit so when you're testing go to demo.localhost:3000/login instead.

Thanks for the repro. I've traced it to when you attempt to pass new headers into NextResponse.rewrite. If you don't override the rewrite headers it appears to function properly. I'll dig in on the Next.js side to see why that's happening.

from next.js.

ztanner avatar ztanner commented on September 13, 2024

Hey @RahulBirCodes upon digging further into it, I think this is expected behavior.

In your reproduction you're taking request headers, adding a new property, and then sending to the client as response headers. However those request headers aren't meant to be sent to the client, as they overwrite things like content-type etc.

If you're wanting to add the x-url response header, you can do so on a new Headers object, rather than cloning the headers from the request. I confirmed that worked as expected, so I'm going to close this out!

from next.js.

github-actions avatar github-actions commented on September 13, 2024

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.

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.