Skip to content

Commit

Permalink
refactor(core): remove middleware from actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Feb 17, 2025
1 parent 3842ce5 commit fd88009
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
7 changes: 1 addition & 6 deletions packages/astro/src/actions/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@ export default function astroIntegrationActionsRouteHandler({
return {
name: VIRTUAL_MODULE_ID,
hooks: {
async 'astro:config:setup'(params) {
async 'astro:config:setup'() {
settings.injectedRoutes.push({
pattern: ACTION_RPC_ROUTE_PATTERN,
entrypoint: 'astro/actions/runtime/route.js',
prerender: false,
origin: 'internal',
});

params.addMiddleware({
entrypoint: 'astro/actions/runtime/middleware.js',
order: 'post',
});
},
'astro:config:done': async (params) => {
if (params.buildOutput === 'static') {
Expand Down
13 changes: 0 additions & 13 deletions packages/astro/src/actions/runtime/middleware.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/astro/src/actions/runtime/virtual/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function unwrapBaseObjectSchema(schema: z.ZodType, unparsedInput: FormData) {
return schema;
}

export type ActionMiddlewareContext = {
export type AstroActionContext = {
/** Information about an incoming action request. */
action?: {
/** Whether an action was called using an RPC function or by using an HTML form action. */
Expand Down Expand Up @@ -268,15 +268,15 @@ export type ActionMiddlewareContext = {
/**
* Access information about Action requests from middleware.
*/
export function getActionContext(context: APIContext): ActionMiddlewareContext {
export function getActionContext(context: APIContext): AstroActionContext {
const callerInfo = getCallerInfo(context);

// Prevents action results from being handled on a rewrite.
// Also prevents our *own* fallback middleware from running
// if the user's middleware has already handled the result.
const actionResultAlreadySet = Boolean((context.locals as Locals)._actionPayload);

let action: ActionMiddlewareContext['action'] = undefined;
let action: AstroActionContext['action'] = undefined;

if (callerInfo && context.request.method === 'POST' && !actionResultAlreadySet) {
action = {
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { type Pipeline, Slots, getParams, getProps } from './render/index.js';
import { isRoute404or500, isRouteExternalRedirect, isRouteServerIsland } from './routing/match.js';
import { copyRequest, getOriginPathname, setOriginPathname } from './routing/rewrite.js';
import { AstroSession } from './session.js';
import {getActionContext} from "../actions/runtime/virtual/server.js";

export const apiContextRoutesSymbol = Symbol.for('context.routes');

Expand Down Expand Up @@ -192,6 +193,16 @@ export class RenderContext {
}
let response: Response;

if (!ctx.isPrerendered) {

const { action, setActionResult, serializeActionResult } = getActionContext(ctx);

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

switch (this.routeData.type) {
case 'endpoint': {
response = await renderEndpoint(
Expand Down
1 change: 0 additions & 1 deletion packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,6 @@ it('Should support trailing slash', async () => {
method: 'POST',
body: formData,
});

assert.equal(res.ok, true);
assert.equal(res.headers.get('Content-Type'), 'application/json+devalue');

Expand Down

0 comments on commit fd88009

Please sign in to comment.