Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actions: remove "action used with get" error #11648

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-tables-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes unexpected error when refreshing a POST request from a form using Actions.
12 changes: 1 addition & 11 deletions packages/astro/src/actions/runtime/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { yellow } from 'kleur/colors';
import type { APIContext, MiddlewareNext } from '../../@types/astro.js';
import {
ActionQueryStringInvalidError,
ActionsUsedWithForGetError,
} from '../../core/errors/errors-data.js';
import { ActionQueryStringInvalidError } from '../../core/errors/errors-data.js';
import { AstroError } from '../../core/errors/errors.js';
import { defineMiddleware } from '../../core/middleware/index.js';
import { formContentTypes, hasContentType } from './utils.js';
Expand Down Expand Up @@ -46,13 +43,6 @@ export const onRequest = defineMiddleware(async (context, next) => {
return handlePost({ context, next, actionName });
}

if (context.request.method === 'GET' && actionName) {
throw new AstroError({
...ActionsUsedWithForGetError,
message: ActionsUsedWithForGetError.message(actionName),
});
}

if (context.request.method === 'POST') {
return handlePostLegacy({ context, next });
}
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ describe('Astro Actions', () => {
assert.equal($('#error-code').text(), 'UNAUTHORIZED');
});

it('Ignores `_astroAction` name for GET requests', async () => {
const req = new Request('http://example.com/user-or-throw?_astroAction=getUserOrThrow', {
method: 'GET',
});
const res = await app.render(req);
assert.equal(res.ok, true);

const html = await res.text();
let $ = cheerio.load(html);
assert.ok($('#user'));
});

describe('legacy', () => {
it('Response middleware fallback', async () => {
const formData = new FormData();
Expand Down
Loading