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

Astro Actions: "Error: Unable to get API context." when is sended arguments to an action. #11423

Closed
1 task
ynhhoJ opened this issue Jul 6, 2024 · 11 comments
Closed
1 task
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@ynhhoJ
Copy link

ynhhoJ commented Jul 6, 2024

Astro Info

Astro                    v4.11.5
Node                     v18.20.3
System                   Linux (x64)
Package Manager          unknown
Output                   server
Adapter                  @astrojs/node
Integrations             @astrojs/preact

If this issue only occurs in one browser, which browser is a problem?

Firefox

Describe the Bug

I followed example from Actions Proposal and everytime when is called an action with arguments like:

await actions.like({ postId })

Is returned error: Uncaught (in promise) Error: Unable to get API context. and 500 HTTP Status

But if we do:

await actions.like()

We get 400 HTTP Status and validation input error.
image

What's the expected result?

There is no error when action:

await actions.like({ postId })

is called

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-pdghi3?file=src%2Fcomponents%2FLike.tsx

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jul 6, 2024
@florian-lefebvre
Copy link
Member

Yeah I think the 500 error should not occur here but be a 400 instead. @bholmesdev wdyt?

@bholmesdev
Copy link
Contributor

Thanks for reporting @ynhhoJ! I just confirmed this a Stackblitz issue, but does not occur locally. Is that true for you as well? That 500 error is indeed unexpected

@bholmesdev bholmesdev self-assigned this Jul 8, 2024
@bholmesdev bholmesdev added the - P3: minor bug An edge case that only affects very specific usage (priority) label Jul 8, 2024
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Jul 8, 2024
@ynhhoJ
Copy link
Author

ynhhoJ commented Jul 8, 2024

@bholmesdev , originally, on on my machine:

Astro                    v4.11.5
Node                     v20.15.0
System                   Linux (x64)
Package Manager          bun
Output                   server
Adapter                  @astrojs/node
Integrations             i18n:custom
                         @astrojs/tailwind
                         @astrojs/preact

I got another error Unexpected end of JSON input, every time when action has a body payload. So this is the cause why I opened this issue.

It's very strange:

Image

image

UPD: This error happens only with bun run --bun dev, if I run project from npm run dev, everything is OK

image

@bholmesdev
Copy link
Contributor

got it. So to clarify @ynhhoJ: do all of the 500 errors you reported go away when using npm? We don't officially support bun, so I'd like to table that dsicussion

@ynhhoJ
Copy link
Author

ynhhoJ commented Jul 8, 2024

do all of the 500 errors you reported go away when using npm?

Yeah. I opened an issue to bun repo.

Thank you!

@ynhhoJ ynhhoJ closed this as completed Jul 8, 2024
@ynhhoJ ynhhoJ reopened this Jul 8, 2024
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jul 8, 2024
@ynhhoJ
Copy link
Author

ynhhoJ commented Jul 8, 2024

Sorry for issue close, maybe 500 error should be fixed by Astro team :D

But Unexpected end of JSON input will be on Bun side

@bholmesdev
Copy link
Contributor

@ynhhoJ Alright got it, that's good info. I know we rely on the node:async_hooks import for API context. It seems Bun has implemented this, but it could be an issue with the combo of Node and Vite. Not sure which side that issue is on yet.

@florian-lefebvre
Copy link
Member

Also stackblitz has issues with ALSs

@bholmesdev
Copy link
Contributor

Using this issue to track errors with edge middleware on Netlify and Vercel. Bun issue has been raised in their repository, and Stackblitz will take cross communication

@bholmesdev
Copy link
Contributor

Alright, we removed async local storage from the Actions implementation, which should help with support in Bun and other environments. That still left your JSON parse error, which I think I've gotten to the bottom of!

We use request.clone().json() to read your input from the Action handler function. For some reason, calling .clone() clears out the response body in Bun (but not other environments). The .clone() is unnecessary and easy to remove on our end. The issue is being tracked in Bun but unfortunately has not seen a fix yet.

@bholmesdev
Copy link
Contributor

@ynhhoJ Well, I'm glad our unit testing suite exists. I've discovered the .clone() is necessary to support reading the ctx.request body directly from an action. I unfortunately needed to close that PR.

I'm hopeful Bun can get this issue resolved on their end! But for now, I suggest removing the --bun flag when using Astro Actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants