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:
parent
413503e52c
commit
ea85546487
3 changed files with 26 additions and 2 deletions
|
@ -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
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue