diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/astro/CHANGELOG.md | 69 | ||||
-rw-r--r-- | packages/astro/package.json | 2 | ||||
-rw-r--r-- | packages/astro/src/actions/integration.ts | 2 | ||||
-rw-r--r-- | packages/astro/src/actions/runtime/virtual/server.ts | 12 |
4 files changed, 77 insertions, 8 deletions
diff --git a/packages/astro/CHANGELOG.md b/packages/astro/CHANGELOG.md index aa3d02e32..2d5bd7070 100644 --- a/packages/astro/CHANGELOG.md +++ b/packages/astro/CHANGELOG.md @@ -1,5 +1,74 @@ # astro +## 5.0.0-beta.8 + +### Minor Changes + +- [#12373](https://github.com/withastro/astro/pull/12373) [`d10f918`](https://github.com/withastro/astro/commit/d10f91815e63f169cff3d1daef5505aef077c76c) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Changes the default behavior for Astro Action form requests to a standard POST submission. + + In Astro 4.x, actions called from an HTML form would trigger a redirect with the result forwarded using cookies. This caused issues for large form errors and return values that exceeded the 4 KB limit of cookie-based storage. + + Astro 5.0 now renders the result of an action as a POST result without any forwarding. This will introduce a "confirm form resubmission?" dialog when a user attempts to refresh the page, though it no longer imposes a 4 KB limit on action return value. + + ## Customize form submission behavior + + If you prefer to address the "confirm form resubmission?" dialog on refresh, or to preserve action results across sessions, you can now [customize action result handling from middleware](https://5-0-0-beta.docs.astro.build/en/guides/actions/#advanced-persist-action-results-with-a-session). + + We recommend using a session storage provider [as described in our Netlify Blob example](https://5-0-0-beta.docs.astro.build/en/guides/actions/#advanced-persist-action-results-with-a-session). However, if you prefer the cookie forwarding behavior from 4.X and accept the 4 KB size limit, you can implement the pattern as shown in this sample snippet: + + ```ts + // src/middleware.ts + import { defineMiddleware } from 'astro:middleware'; + import { getActionContext } from 'astro:actions'; + + export const onRequest = defineMiddleware(async (context, next) => { + // Skip requests for prerendered pages + if (context.isPrerendered) return next(); + + const { action, setActionResult, serializeActionResult } = getActionContext(context); + + // If an action result was forwarded as a cookie, set the result + // to be accessible from `Astro.getActionResult()` + const payload = context.cookies.get('ACTION_PAYLOAD'); + if (payload) { + const { actionName, actionResult } = payload.json(); + setActionResult(actionName, actionResult); + context.cookies.delete('ACTION_PAYLOAD'); + return next(); + } + + // If an action was called from an HTML form action, + // call the action handler and redirect with the result as a cookie. + if (action?.calledFrom === 'form') { + const actionResult = await action.handler(); + + context.cookies.set('ACTION_PAYLOAD', { + actionName: action.name, + actionResult: serializeActionResult(actionResult), + }); + + if (actionResult.error) { + // Redirect back to the previous page on error + const referer = context.request.headers.get('Referer'); + if (!referer) { + throw new Error('Internal: Referer unexpectedly missing from Action POST request.'); + } + return context.redirect(referer); + } + // Redirect to the destination page on success + return context.redirect(context.originPathname); + } + + return next(); + }); + ``` + +### Patch Changes + +- [#12339](https://github.com/withastro/astro/pull/12339) [`bdb75a8`](https://github.com/withastro/astro/commit/bdb75a87f24d7f032797483164fb2f82aa691fee) Thanks [@ematipico](https://github.com/ematipico)! - Adds an error when `Astro.rewrite()` is used to rewrite an on-demand route with a static route when using the `"server"` output. + + This is a forbidden rewrite because Astro can't retrieve the emitted static route at runtime. This route is served by the hosting platform, and not Astro itself. + ## 5.0.0-beta.7 ### Minor Changes diff --git a/packages/astro/package.json b/packages/astro/package.json index a678ba60b..d29b01932 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -1,6 +1,6 @@ { "name": "astro", - "version": "5.0.0-beta.7", + "version": "5.0.0-beta.8", "description": "Astro is a modern site builder with web best practices, performance, and DX front-of-mind.", "type": "module", "author": "withastro", diff --git a/packages/astro/src/actions/integration.ts b/packages/astro/src/actions/integration.ts index 23fbd904a..d7c99fc42 100644 --- a/packages/astro/src/actions/integration.ts +++ b/packages/astro/src/actions/integration.ts @@ -3,7 +3,7 @@ import { AstroError } from '../core/errors/errors.js'; import { viteID } from '../core/util.js'; import type { AstroSettings } from '../types/astro.js'; import type { AstroIntegration } from '../types/public/integrations.js'; -import { ACTIONS_TYPES_FILE, VIRTUAL_MODULE_ID, ACTION_RPC_ROUTE_PATTERN } from './consts.js'; +import { ACTIONS_TYPES_FILE, ACTION_RPC_ROUTE_PATTERN, VIRTUAL_MODULE_ID } from './consts.js'; /** * This integration is applied when the user is using Actions in their project. diff --git a/packages/astro/src/actions/runtime/virtual/server.ts b/packages/astro/src/actions/runtime/virtual/server.ts index f8fac557a..f28b3c239 100644 --- a/packages/astro/src/actions/runtime/virtual/server.ts +++ b/packages/astro/src/actions/runtime/virtual/server.ts @@ -1,13 +1,17 @@ import { z } from 'zod'; import { ActionCalledFromServerError } from '../../../core/errors/errors-data.js'; import { AstroError } from '../../../core/errors/errors.js'; +import type { APIContext } from '../../../types/public/index.js'; +import { ACTION_RPC_ROUTE_PATTERN } from '../../consts.js'; import { - formContentTypes, - hasContentType, type ActionAPIContext, type ErrorInferenceObject, type MaybePromise, + formContentTypes, + hasContentType, } from '../utils.js'; +import type { Locals } from '../utils.js'; +import { getAction } from './get-action.js'; import { ACTION_QUERY_PARAMS, ActionError, @@ -18,10 +22,6 @@ import { deserializeActionResult, serializeActionResult, } from './shared.js'; -import type { Locals } from '../utils.js'; -import { getAction } from './get-action.js'; -import type { APIContext } from '../../../types/public/index.js'; -import { ACTION_RPC_ROUTE_PATTERN } from '../../consts.js'; export * from './shared.js'; |