middleware.ts — astro Source File
Architecture documentation for middleware.ts, a typescript file in the astro codebase. 2 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR aad54dea_07c4_5cee_1fb5_1e37b6981c31["middleware.ts"] 3a7a7ff7_6c64_95f9_af85_4deb518834f1["astro:middleware"] aad54dea_07c4_5cee_1fb5_1e37b6981c31 --> 3a7a7ff7_6c64_95f9_af85_4deb518834f1 58a2600d_f5df_9651_e0d8_9010ddeef24d["astro:actions"] aad54dea_07c4_5cee_1fb5_1e37b6981c31 --> 58a2600d_f5df_9651_e0d8_9010ddeef24d style aad54dea_07c4_5cee_1fb5_1e37b6981c31 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import { defineMiddleware } from 'astro:middleware';
import { getActionContext } from 'astro:actions';
const ACTION_SESSION_KEY = 'actionResult'
export const onRequest = defineMiddleware(async (context, next) => {
// Skip requests for prerendered pages
if (context.isPrerendered) return next();
const { action, setActionResult, serializeActionResult } =
getActionContext(context);
console.log(action?.name)
const actionPayload = await context.session.get(ACTION_SESSION_KEY);
if (actionPayload) {
setActionResult(actionPayload.actionName, actionPayload.actionResult);
context.session.delete(ACTION_SESSION_KEY);
return next();
}
// If an action was called from an HTML form action,
// call the action handler and redirect to the destination page
if (action?.calledFrom === "form") {
const actionResult = await action.handler();
context.session.set(ACTION_SESSION_KEY, {
actionName: action.name,
actionResult: serializeActionResult(actionResult),
});
// Redirect back to the previous page on error
if (actionResult.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();
});
Domain
Subdomains
Functions
Dependencies
- astro:actions
- astro:middleware
Source
Frequently Asked Questions
What does middleware.ts do?
middleware.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in middleware.ts?
middleware.ts defines 1 function(s): onRequest.
What does middleware.ts depend on?
middleware.ts imports 2 module(s): astro:actions, astro:middleware.
Where is middleware.ts in the architecture?
middleware.ts is located at packages/integrations/cloudflare/test/fixtures/sessions/src/middleware.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/cloudflare/test/fixtures/sessions/src).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free