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

Actions middleware #12373

Merged
merged 39 commits into from
Nov 8, 2024
Merged

Actions middleware #12373

merged 39 commits into from
Nov 8, 2024

Conversation

bholmesdev
Copy link
Contributor

@bholmesdev bholmesdev commented Nov 4, 2024

Change Action cookie redirects to an opt-in feature users can implement using middleware. This means Actions submitted from an HTML form action will no longer redirect to the destination as a GET request. Instead, the page will be rendered as a POST result.

This is meant to address the 4 KB size limit users have encountered when calling actions from an HTML form action. It can be difficult to predict the size of an action result for large validation errors or longer return values, like AI chatbot results.

So, we've introduced a new getActionContext() utility to let you decide how action results are handled from middleware. You may choose to implement the existing cookie redirect from Astro v4, or implement forwarding with your own session storage.

Here is an example of how our built-in POST middleware works. This calls the appropriate action handler and sets the result to be accessible from Astro.getActionResult():

export const onRequest = defineMiddleware(async (context, next) => {
	if (context.isPrerendered) return next();
	const { action, setActionResult, serializeActionResult } = getActionContext(context);

	if (action?.calledFrom === 'form') {
		const actionResult = await action.handler();
		setActionResult(action.name, serializeActionResult(actionResult));
	}
	return next();
});

See the changeset for a sample implementation of cookie forwarding from Astro v4.

Changes

  • Introduce a getActionContext() utility that exposes utilities to get an action request and set the result
  • Update runtime/middleware and runtime/route to dogfood this function
  • Remove all cookie forwarding behavior from the previous middleware
  • Expose new context.originPathname property. This is used to retrieve the original pathname through any rewrites. Needed for users to DIY cookie forwarding, and should be useful for middleware authors in general.

Testing

  • Add integration and e2e tests for DIY cookie redirect implementation
  • Add test for RPC security gate

Docs

withastro/docs#9943

Copy link

changeset-bot bot commented Nov 4, 2024

🦋 Changeset detected

Latest commit: cba1e11

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) docs pr and removed docs pr labels Nov 4, 2024
@bholmesdev bholmesdev changed the base branch from main to next November 5, 2024 18:24
@github-actions github-actions bot added the semver: minor Change triggers a `minor` release label Nov 5, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

@bholmesdev bholmesdev marked this pull request as ready for review November 5, 2024 22:31
@bholmesdev bholmesdev changed the title DRAFT: Actions middleware Actions middleware Nov 5, 2024
Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good! I like the compromise of adding getActionContext.

@bholmesdev
Copy link
Contributor Author

!preview astro-actions-middleware

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job Ben!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this will make people super happy @bholmesdev ! Just doing my docs diligence (will be getting through the guide material later today) and dropping some thoughts re: the changeset.

Also noting that I don't see any new error messages here, so confirming no new ones were added that I should be looking at! 🙌

.changeset/tall-waves-impress.md Outdated Show resolved Hide resolved
.changeset/tall-waves-impress.md Outdated Show resolved Hide resolved
.changeset/tall-waves-impress.md Outdated Show resolved Hide resolved
@bholmesdev bholmesdev merged commit d10f918 into next Nov 8, 2024
14 checks passed
@bholmesdev bholmesdev deleted the actions-middleware branch November 8, 2024 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs pr pkg: astro Related to the core `astro` package (scope) semver: minor Change triggers a `minor` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants