0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

Fix base not working with server actions (#12280)

Co-authored-by: Fredrik Löwenhamn <fredrik.lowenhamn@gmail.com>
This commit is contained in:
Fredrik Löwenhamn 2024-10-30 12:55:39 +01:00 committed by GitHub
parent 413503e52c
commit ea85546487
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 2 deletions

View file

@ -12,7 +12,7 @@ export async function getAction(
path: string, path: string,
): Promise<ActionClient<unknown, ActionAccept, ZodType>> { ): Promise<ActionClient<unknown, ActionAccept, ZodType>> {
const pathKeys = path const pathKeys = path
.replace('/_actions/', '') .replace(/^.*\/_actions\//, '')
.split('.') .split('.')
.map((key) => decodeURIComponent(key)); .map((key) => decodeURIComponent(key));
// @ts-expect-error virtual module // @ts-expect-error virtual module

View file

@ -92,7 +92,7 @@ async function handleAction(param, path, context) {
headers.set('Content-Length', '0'); headers.set('Content-Length', '0');
} }
} }
const rawResult = await fetch(`/_actions/${path}`, { const rawResult = await fetch(`${import.meta.env.BASE_URL.replace(/\/$/, "")}/_actions/${path}`, {
method: 'POST', method: 'POST',
body, body,
headers, headers,

View file

@ -466,6 +466,30 @@ describe('Astro Actions', () => {
}); });
}); });
it('Base path should be used', async () => {
const fixture = await loadFixture({
root: './fixtures/actions/',
adapter: testAdapter(),
base: "/base"
});
const devServer = await fixture.startDevServer();
const formData = new FormData();
formData.append('channel', 'bholmesdev');
formData.append('comment', 'Hello, World!');
const res = await fixture.fetch('/base/_actions/comment', {
method: 'POST',
body: formData,
});
assert.equal(res.ok, true);
assert.equal(res.headers.get('Content-Type'), 'application/json+devalue');
const data = devalue.parse(await res.text());
assert.equal(data.channel, 'bholmesdev');
assert.equal(data.comment, 'Hello, World!');
await devServer.stop()
});
/** /**
* Follow an expected redirect response. * Follow an expected redirect response.
* *