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

Context not available in astro:actions #12393

Closed
1 task
mariusbolik opened this issue Nov 7, 2024 · 3 comments · Fixed by #12402
Closed
1 task

Context not available in astro:actions #12393

mariusbolik opened this issue Nov 7, 2024 · 3 comments · Fixed by #12402
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) feat: actions Related to Astro actions (scope)

Comments

@mariusbolik
Copy link

mariusbolik commented Nov 7, 2024

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

  • 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 Nov 7, 2024
@Klerith
Copy link

Klerith commented Nov 7, 2024

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
Copy link
Author

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

@ematipico ematipico added - P4: important Violate documented behavior or significantly impacts performance (priority) feat: actions Related to Astro actions (scope) labels Nov 8, 2024
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Nov 8, 2024
@ematipico ematipico self-assigned this Nov 8, 2024
@ematipico ematipico removed the - P4: important Violate documented behavior or significantly impacts performance (priority) label Nov 8, 2024
@bholmesdev
Copy link
Contributor

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!

@ematipico ematipico added the - P3: minor bug An edge case that only affects very specific usage (priority) label Nov 8, 2024
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) feat: actions Related to Astro actions (scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants