Skip to content

Commit

Permalink
nit: ctx -> context
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Nov 7, 2024
1 parent c8151be commit fe72372
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions .changeset/tall-waves-impress.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ Cookie redirects are no longer handled by default. You can implement the behavio
import { defineMiddleware } from 'astro:middleware';
import { getActionContext } from 'astro:actions';

export const onRequest = defineMiddleware(async (ctx, next) => {
if (ctx.isPrerendered) return next();
export const onRequest = defineMiddleware(async (context, next) => {
if (context.isPrerendered) return next();

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

// If an action result was forwarded as a cookie, set the result
// to be accessible from `Astro.getActionResult()`
const payload = ctx.cookies.get('ACTION_PAYLOAD');
const payload = context.cookies.get('ACTION_PAYLOAD');
if (payload) {
const { actionName, actionResult } = payload.json();
setActionResult(actionName, actionResult);
ctx.cookies.delete('ACTION_PAYLOAD');
context.cookies.delete('ACTION_PAYLOAD');
return next();
}

Expand All @@ -36,21 +36,21 @@ export const onRequest = defineMiddleware(async (ctx, next) => {
if (action?.calledFrom === 'form') {
const actionResult = await action.handler();

ctx.cookies.set('ACTION_PAYLOAD', {
context.cookies.set('ACTION_PAYLOAD', {
actionName: action.name,
actionResult: serializeActionResult(actionResult),
});

if (actionResult.error) {
// Redirect back to the previous page on error
const referer = ctx.request.headers.get('Referer');
const referer = context.request.headers.get('Referer');
if (!referer) {
throw new Error('Internal: Referer unexpectedly missing from Action POST request.');
}
return ctx.redirect(referer);
return context.redirect(referer);
}
// Redirect to the destination page on success
return ctx.redirect(ctx.originPathname);
return context.redirect(context.originPathname);
}

return next();
Expand Down

0 comments on commit fe72372

Please sign in to comment.