Add middleware to persist form actions in session and follow POST-REDIRECT-GET
form submission pattern
This commit is contained in:
parent
f2f42a84b5
commit
f7bdfd3cb8
1 changed files with 46 additions and 0 deletions
46
src/middleware.ts
Normal file
46
src/middleware.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { defineMiddleware } from "astro:middleware";
|
||||
import { getActionContext } from "astro:actions";
|
||||
import { randomUUID } from "node:crypto";
|
||||
|
||||
export const onRequest = defineMiddleware(async (context, next) => {
|
||||
if (context.isPrerendered) return next();
|
||||
|
||||
const { action, setActionResult, serializeActionResult } =
|
||||
getActionContext(context);
|
||||
|
||||
const currentAction = await context.session?.get("currentAction");
|
||||
|
||||
if (currentAction) {
|
||||
const { actionName, actionResult } = JSON.parse(currentAction);
|
||||
setActionResult(actionName, actionResult);
|
||||
|
||||
context.session?.delete("currentAction");
|
||||
return next();
|
||||
}
|
||||
|
||||
if (action?.calledFrom === "form") {
|
||||
const actionResult = await action.handler();
|
||||
|
||||
context.session?.set(
|
||||
"currentAction",
|
||||
JSON.stringify({
|
||||
actionName: action.name,
|
||||
actionResult: serializeActionResult(actionResult),
|
||||
}),
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return context.redirect(context.originPathname);
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue