0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-13 22:11:20 -05:00
astro/.changeset/many-mayflies-enjoy.md
Ben Holmes a65ffe314b
Actions: Remove async local storage dependency (#11584)
* refactor: move from Async local storage to .bind()

* feat: Astro.callAction for server calls

* feat(e2e): update server call

* feat(types): test callAction types

* fix: callAction context on rewrite

* feat: called from server error

* chore: remove store.ts file

* chore: remove getApiContext client stub

* chore: changeset

* fix: src -> dist import

* refactor: move content collection errors together

* fix: move rfc link to hint
2024-08-01 08:24:22 -04:00

1.6 KiB

astro
patch

Removes async local storage dependency from Astro Actions. This allows Actions to run in Cloudflare and Stackblitz without opt-in flags or other configuration.

This also introduces a new convention for calling actions from server code. Instead of calling actions directly, you must wrap function calls with the new Astro.callAction() utility.

callAction() is meant to trigger an action from server code. getActionResult() usage with form submissions remains unchanged.

---
import { actions } from 'astro:actions';

const result = await Astro.callAction(actions.searchPosts, {
  searchTerm: Astro.url.searchParams.get('search'),
});
---

{result.data && (
  {/* render the results */}
)}

Migration

If you call actions directly from server code, update function calls to use the Astro.callAction() wrapper for pages and context.callAction() for endpoints:

---
import { actions } from 'astro:actions';

- const result = await actions.searchPosts({ searchTerm: 'test' });
+ const result = await Astro.callAction(actions.searchPosts, { searchTerm: 'test' });
---

If you deploy with Cloudflare and added the nodejs_compat or nodejs_als flags for Actions, we recommend removing these:

compatibility_flags = [
- "nodejs_compat",
- "nodejs_als"
]

You can also remove node:async_hooks from the vite.ssr.external option in your astro.config file:

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
- vite: {
-   ssr: {
-     external: ["node:async_hooks"]
-   }
- }
})