From 84189b6511dc2a14bcfe608696f56a64c2046f39 Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Tue, 30 Jul 2024 11:04:10 -0400 Subject: [PATCH] Actions: New fallback behavior with `action={actions.name}` (#11570) * feat: support _astroAction query param * feat(test): _astroAction query param * fix: handle _actions requests from legacy fallback * feat(e2e): new actions pattern on blog test * feat: update React 19 adapter to use query params * fix: remove legacy getApiContext() * feat: ActionQueryStringInvalidError * fix: update error description * feat: ActionQueryStringInvalidError * chore: comment on _actions skip * feat: .queryString property * chore: comment on throw new Error * chore: better guess for "why" on query string * chore: remove console log * chore: changeset * chore: changeset --- .changeset/silly-bulldogs-sparkle.md | 53 ++++++++ .../src/pages/blog/[...slug].astro | 5 +- .../actions-react-19/src/actions/index.ts | 5 +- .../astro/src/actions/runtime/middleware.ts | 124 ++++++++++++++---- .../src/actions/runtime/virtual/server.ts | 5 +- .../src/actions/runtime/virtual/shared.ts | 19 ++- packages/astro/src/core/errors/errors-data.ts | 30 +++++ packages/astro/templates/actions.mjs | 27 ++-- packages/astro/test/actions.test.js | 48 +++++-- packages/integrations/react/server.js | 7 +- packages/integrations/react/src/actions.ts | 5 +- 11 files changed, 269 insertions(+), 59 deletions(-) create mode 100644 .changeset/silly-bulldogs-sparkle.md diff --git a/.changeset/silly-bulldogs-sparkle.md b/.changeset/silly-bulldogs-sparkle.md new file mode 100644 index 000000000000..9b23a675fa53 --- /dev/null +++ b/.changeset/silly-bulldogs-sparkle.md @@ -0,0 +1,53 @@ +--- +'@astrojs/react': patch +'astro': patch +--- + +**BREAKING CHANGE to the experimental Actions API only.** Install the latest `@astrojs/react` integration as well if you're using React 19 features. + +Updates the Astro Actions fallback to support `action={actions.name}` instead of using `getActionProps().` This will submit a form to the server in zero-JS scenarios using a search parameter: + +```astro +--- +import { actions } from 'astro:actions'; +--- + +
+ + +
+``` + +You may also construct form action URLs using string concatenation, or by using the `URL()` constructor, with the an action's `.queryString` property: + +```astro +--- +import { actions } from 'astro:actions'; + +const confirmationUrl = new URL('/confirmation', Astro.url); +confirmationUrl.search = actions.queryString; +--- + +
+ +
+``` + +## Migration + +`getActionProps()` is now deprecated. To use the new fallback pattern, remove the `getActionProps()` input from your form and pass your action function to the form `action` attribute: + +```diff +--- +import { + actions, +- getActionProps, +} from 'astro:actions'; +--- + ++
+- +- + +
+``` diff --git a/packages/astro/e2e/fixtures/actions-blog/src/pages/blog/[...slug].astro b/packages/astro/e2e/fixtures/actions-blog/src/pages/blog/[...slug].astro index e864c36101be..64deb7a46b43 100644 --- a/packages/astro/e2e/fixtures/actions-blog/src/pages/blog/[...slug].astro +++ b/packages/astro/e2e/fixtures/actions-blog/src/pages/blog/[...slug].astro @@ -4,7 +4,7 @@ import BlogPost from '../../layouts/BlogPost.astro'; import { db, eq, Comment, Likes } from 'astro:db'; import { Like } from '../../components/Like'; import { PostComment } from '../../components/PostComment'; -import { actions, getActionProps } from 'astro:actions'; +import { actions } from 'astro:actions'; import { isInputError } from 'astro:actions'; export const prerender = false; @@ -55,8 +55,7 @@ const commentPostIdOverride = Astro.url.searchParams.get('commentPostIdOverride' : undefined} client:load /> -
- +