onRequest() — astro Function Reference
Architecture documentation for the onRequest() function in middleware.ts from the astro codebase.
Entity Profile
Dependency Diagram
graph TD b323c8a2_c713_fe26_4822_d263eda3ada9["onRequest()"] aad54dea_07c4_5cee_1fb5_1e37b6981c31["middleware.ts"] b323c8a2_c713_fe26_4822_d263eda3ada9 -->|defined in| aad54dea_07c4_5cee_1fb5_1e37b6981c31 style b323c8a2_c713_fe26_4822_d263eda3ada9 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/integrations/cloudflare/test/fixtures/sessions/src/middleware.ts lines 6–49
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
Source
Frequently Asked Questions
What does onRequest() do?
onRequest() is a function in the astro codebase, defined in packages/integrations/cloudflare/test/fixtures/sessions/src/middleware.ts.
Where is onRequest() defined?
onRequest() is defined in packages/integrations/cloudflare/test/fixtures/sessions/src/middleware.ts at line 6.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free