Skip to content

Commit

Permalink
fix: check cookie after handling POST requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 9, 2024
1 parent 52aab84 commit 607f90f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/astro/src/actions/runtime/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ export const onRequest = defineMiddleware(async (context, next) => {
// so short circuit if already defined.
if (locals._actionPayload) return next();

const actionPayload = context.cookies.get(ACTION_QUERY_PARAMS.actionPayload)?.json();
if (actionPayload) {
if (!isActionPayload(actionPayload)) {
throw new Error('Internal: Invalid action payload in cookie.');
}
return renderResult({ context, next, ...actionPayload });
}

// Heuristic: If body is null, Astro might've reset this for prerendering.
if (import.meta.env.DEV && request.method === 'POST' && request.body === null) {
// eslint-disable-next-line no-console
Expand All @@ -58,6 +50,14 @@ export const onRequest = defineMiddleware(async (context, next) => {
return handlePostLegacy({ context, next });
}

const actionPayload = context.cookies.get(ACTION_QUERY_PARAMS.actionPayload)?.json();
if (context.request.method === 'GET' && actionPayload) {
if (!isActionPayload(actionPayload)) {
throw new Error('Internal: Invalid action payload in cookie.');
}
return renderResult({ context, next, ...actionPayload });
}

return next();
});

Expand Down

0 comments on commit 607f90f

Please sign in to comment.