withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.74k stars 2.48k forks source link

Context not available in `astro:actions` #12393

Closed mariusbolik closed 4 days ago

mariusbolik commented 5 days ago

Astro Info

Astro                    v4.16.7
Node                     v20.15.0
System                   macOS (arm64)
Package Manager          bun
Output                   hybrid
Adapter                  @astrojs/cloudflare
Integrations             none

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

No response

Describe the Bug

A am using astro with the cloudflare adapter and output: 'hybrid'. I can't access methods from the ctx object without making the dev server crash. I receive the following error message:

AstroError [ActionNotFoundError]: The server received a request for an action named `url.searchParams.get` but could not find a match. If you renamed an action, check that you've updated your `actions/index` file and your calling code to match.
    at getAction (/Users/marius/Projects/Company/my-project/node_modules/astro/dist/actions/runtime/virtual/get-action.js:17:13)
    at async handleAction (astro:actions:68:18) {
  loc: undefined,
  title: 'Action not found.',
  hint: 'You can run `astro check` to detect type errors caused by mismatched action names.',
  frame: undefined,
  type: 'AstroError'
}

Same with ctx.cookies.get.

Here are the console logs of the ctx object:

ctx: {}
ctx.cookies: [Function: action] {
  queryString: '?_astroAction=cookies',
  toString: [Function: toString],
  '$$FORM_ACTION': [Function: $$FORM_ACTION],
  orThrow: [AsyncFunction: orThrow]
}
ctx.url: [Function: action] {
  queryString: '?_astroAction=url',
  toString: [Function: toString],
  '$$FORM_ACTION': [Function: $$FORM_ACTION],
  orThrow: [AsyncFunction: orThrow]
}

What's the expected result?

I would like to access the methods of the ctx object to read and write cookies.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-zqu2ga?file=src%2Fpages%2Findex.astro,src%2Factions%2Findex.ts&on=stackblitz

Participation

Klerith commented 5 days ago

I'm facing the same issue. When I try to get my cookies from action, I get the same object that doesn't contain the has, get and all the other methods from cookies.

cookies: [Function: action] {
    queryString: '?_astroAction=cookies',
    toString: [Function: toString],
    '$$FORM_ACTION': [Function: $$FORM_ACTION],
    orThrow: [AsyncFunction: orThrow]
  }

The code was working perfectly when the actions where in beta.

mariusbolik commented 5 days ago

Good point @Klerith! I also remember that it used to work back when actions were an experimental feature.

bholmesdev commented 4 days ago

Hey @Klerith and @mariusbolik! Thanks for reporting. This is actually expected behavior, but the error message definitely doesn't steer you in the right direction.

We made the decision to remove our "async local storage" requirement from Actions before release. This came with a consequence: you need to use the Astro.callAction() wrapper when calling actions from Astro frontmatter. This ensures that context is correctly injected.

Can you try updating your code to use Astro.callAction as documented here? We'll be adding this instruction to the Actions guide to make this more visible!